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