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