50f3af643acc8cdf4ba80ed6f20efa65ff767432
[platform/upstream/cryptsetup.git] / lib / utils_wipe.c
1 /*
2  * utils_wipe - wipe a device
3  *
4  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
5  * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
6  * Copyright (C) 2009-2012, Milan Broz
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <fcntl.h>
32
33 #include "libcryptsetup.h"
34 #include "internal.h"
35
36 #define MAXIMUM_WIPE_BYTES      1024 * 1024 * 32 /* 32 MiB */
37
38 static ssize_t _crypt_wipe_zero(int fd, int bsize, char *buffer,
39                                 uint64_t offset, uint64_t size)
40 {
41         memset(buffer, 0, size);
42         return write_lseek_blockwise(fd, bsize, buffer, size, offset);
43 }
44
45 static ssize_t _crypt_wipe_random(int fd, int bsize, char *buffer,
46                                   uint64_t offset, uint64_t size)
47 {
48         if (crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL) < 0)
49                 return -EINVAL;
50
51         return write_lseek_blockwise(fd, bsize, buffer, size, offset);
52 }
53
54 /*
55  * Wipe using Peter Gutmann method described in
56  * http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
57  */
58 static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
59 {
60         unsigned int i;
61
62         unsigned char write_modes[][3] = {
63                 {"\x55\x55\x55"}, {"\xaa\xaa\xaa"}, {"\x92\x49\x24"},
64                 {"\x49\x24\x92"}, {"\x24\x92\x49"}, {"\x00\x00\x00"},
65                 {"\x11\x11\x11"}, {"\x22\x22\x22"}, {"\x33\x33\x33"},
66                 {"\x44\x44\x44"}, {"\x55\x55\x55"}, {"\x66\x66\x66"},
67                 {"\x77\x77\x77"}, {"\x88\x88\x88"}, {"\x99\x99\x99"},
68                 {"\xaa\xaa\xaa"}, {"\xbb\xbb\xbb"}, {"\xcc\xcc\xcc"},
69                 {"\xdd\xdd\xdd"}, {"\xee\xee\xee"}, {"\xff\xff\xff"},
70                 {"\x92\x49\x24"}, {"\x49\x24\x92"}, {"\x24\x92\x49"},
71                 {"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
72         };
73
74         for(i = 0; i < buffer_size / 3; ++i) {
75                 memcpy(buffer, write_modes[turn], 3);
76                 buffer += 3;
77         }
78 }
79
80 static ssize_t _crypt_wipe_disk(int fd, int bsize, char *buffer,
81                                 uint64_t offset, uint64_t size)
82 {
83         int r;
84         unsigned int i;
85         ssize_t written;
86
87         for(i = 0; i < 39; ++i) {
88                 if (i <  5) {
89                         r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
90                 } else if(i >=  5 && i < 32) {
91                         wipeSpecial(buffer, size, i - 5);
92                         r = 0;
93                 } else if(i >= 32 && i < 38) {
94                         r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
95                 } else if(i >= 38 && i < 39) {
96                         memset(buffer, 0xFF, size);
97                         r = 0;
98                 }
99                 if (r < 0)
100                         return r;
101
102                 written = write_lseek_blockwise(fd, bsize, buffer, size, offset);
103                 if (written < 0 || written != (ssize_t)size)
104                         return written;
105         }
106
107         /* Rewrite it finally with random */
108         return _crypt_wipe_random(fd, bsize, buffer, offset, size);
109 }
110
111 static ssize_t _crypt_wipe_ssd(int fd, int bsize, char *buffer,
112                                uint64_t offset, uint64_t size)
113 {
114         // FIXME: for now just rewrite it by random
115         return _crypt_wipe_random(fd, bsize, buffer, offset, size);
116 }
117
118 int crypt_wipe(struct device *device,
119                uint64_t offset,
120                uint64_t size,
121                crypt_wipe_type type,
122                int exclusive)
123 {
124         struct stat st;
125         char *buffer;
126         int devfd, flags, rotational, bsize;
127         ssize_t written;
128
129         if (!size || size % SECTOR_SIZE || (size > MAXIMUM_WIPE_BYTES)) {
130                 log_dbg("Unsuported wipe size for device %s: %ld.",
131                         device_path(device), (unsigned long)size);
132                 return -EINVAL;
133         }
134
135         if (stat(device_path(device), &st) < 0) {
136                 log_dbg("Device %s not found.", device_path(device));
137                 return -EINVAL;
138         }
139
140         if (type == CRYPT_WIPE_DISK && S_ISBLK(st.st_mode)) {
141                 rotational = 0;
142                 if (!crypt_sysfs_get_rotational(major(st.st_rdev),
143                                                 minor(st.st_rdev),
144                                                 &rotational))
145                         rotational = 1;
146                 log_dbg("Rotational flag is %d.", rotational);
147                 if (!rotational)
148                         type = CRYPT_WIPE_SSD;
149         }
150
151         bsize = device_block_size(device);
152         if (bsize <= 0)
153                 return -EINVAL;
154
155         buffer = malloc(size);
156         if (!buffer)
157                 return -ENOMEM;
158
159         flags = O_RDWR | O_DIRECT | O_SYNC;
160
161         /* use O_EXCL only for block devices */
162         if (exclusive && S_ISBLK(st.st_mode))
163                 flags |= O_EXCL;
164
165         /* coverity[toctou] */
166         devfd = open(device_path(device), flags);
167         if (devfd == -1) {
168                 free(buffer);
169                 return errno ? -errno : -EINVAL;
170         }
171
172         // FIXME: use fixed block size and loop here
173         switch (type) {
174                 case CRYPT_WIPE_ZERO:
175                         written = _crypt_wipe_zero(devfd, bsize, buffer, offset, size);
176                         break;
177                 case CRYPT_WIPE_DISK:
178                         written = _crypt_wipe_disk(devfd, bsize, buffer, offset, size);
179                         break;
180                 case CRYPT_WIPE_SSD:
181                         written = _crypt_wipe_ssd(devfd, bsize, buffer, offset, size);
182                         break;
183                 case CRYPT_WIPE_RANDOM:
184                         written = _crypt_wipe_random(devfd, bsize, buffer, offset, size);
185                         break;
186                 default:
187                         log_dbg("Unsuported wipe type requested: (%d)", type);
188                         written = -1;
189         }
190
191         close(devfd);
192         free(buffer);
193
194         if (written != (ssize_t)size || written < 0)
195                 return -EIO;
196
197         return 0;
198 }