Include 'sys/sysmacros.h' for GCC-9
[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 #ifdef HAVE_SYS_SYSMACROS_H
34 #include <sys/sysmacros.h>     /* for major, minor */
35 #endif
36
37 #include "libcryptsetup.h"
38 #include "internal.h"
39
40 #define MAXIMUM_WIPE_BYTES      1024 * 1024 * 32 /* 32 MiB */
41
42 static ssize_t _crypt_wipe_zero(int fd, int bsize, char *buffer,
43                                 uint64_t offset, uint64_t size)
44 {
45         memset(buffer, 0, size);
46         return write_lseek_blockwise(fd, bsize, buffer, size, offset);
47 }
48
49 static ssize_t _crypt_wipe_random(int fd, int bsize, char *buffer,
50                                   uint64_t offset, uint64_t size)
51 {
52         if (crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL) < 0)
53                 return -EINVAL;
54
55         return write_lseek_blockwise(fd, bsize, buffer, size, offset);
56 }
57
58 /*
59  * Wipe using Peter Gutmann method described in
60  * http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
61  */
62 static void wipeSpecial(char *buffer, size_t buffer_size, unsigned int turn)
63 {
64         unsigned int i;
65
66         unsigned char write_modes[][3] = {
67                 {"\x55\x55\x55"}, {"\xaa\xaa\xaa"}, {"\x92\x49\x24"},
68                 {"\x49\x24\x92"}, {"\x24\x92\x49"}, {"\x00\x00\x00"},
69                 {"\x11\x11\x11"}, {"\x22\x22\x22"}, {"\x33\x33\x33"},
70                 {"\x44\x44\x44"}, {"\x55\x55\x55"}, {"\x66\x66\x66"},
71                 {"\x77\x77\x77"}, {"\x88\x88\x88"}, {"\x99\x99\x99"},
72                 {"\xaa\xaa\xaa"}, {"\xbb\xbb\xbb"}, {"\xcc\xcc\xcc"},
73                 {"\xdd\xdd\xdd"}, {"\xee\xee\xee"}, {"\xff\xff\xff"},
74                 {"\x92\x49\x24"}, {"\x49\x24\x92"}, {"\x24\x92\x49"},
75                 {"\x6d\xb6\xdb"}, {"\xb6\xdb\x6d"}, {"\xdb\x6d\xb6"}
76         };
77
78         for(i = 0; i < buffer_size / 3; ++i) {
79                 memcpy(buffer, write_modes[turn], 3);
80                 buffer += 3;
81         }
82 }
83
84 static ssize_t _crypt_wipe_disk(int fd, int bsize, char *buffer,
85                                 uint64_t offset, uint64_t size)
86 {
87         int r;
88         unsigned int i;
89         ssize_t written;
90
91         for(i = 0; i < 39; ++i) {
92                 if (i <  5) {
93                         r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
94                 } else if(i >=  5 && i < 32) {
95                         wipeSpecial(buffer, size, i - 5);
96                         r = 0;
97                 } else if(i >= 32 && i < 38) {
98                         r = crypt_random_get(NULL, buffer, size, CRYPT_RND_NORMAL);
99                 } else if(i >= 38 && i < 39) {
100                         memset(buffer, 0xFF, size);
101                         r = 0;
102                 }
103                 if (r < 0)
104                         return r;
105
106                 written = write_lseek_blockwise(fd, bsize, buffer, size, offset);
107                 if (written < 0 || written != (ssize_t)size)
108                         return written;
109         }
110
111         /* Rewrite it finally with random */
112         return _crypt_wipe_random(fd, bsize, buffer, offset, size);
113 }
114
115 static ssize_t _crypt_wipe_ssd(int fd, int bsize, char *buffer,
116                                uint64_t offset, uint64_t size)
117 {
118         // FIXME: for now just rewrite it by random
119         return _crypt_wipe_random(fd, bsize, buffer, offset, size);
120 }
121
122 int crypt_wipe(struct device *device,
123                uint64_t offset,
124                uint64_t size,
125                crypt_wipe_type type,
126                int exclusive)
127 {
128         struct stat st;
129         char *buffer;
130         int devfd, flags, bsize;
131         ssize_t written;
132
133         if (!size || size % SECTOR_SIZE || (size > MAXIMUM_WIPE_BYTES)) {
134                 log_dbg("Unsuported wipe size for device %s: %ld.",
135                         device_path(device), (unsigned long)size);
136                 return -EINVAL;
137         }
138
139         if (stat(device_path(device), &st) < 0) {
140                 log_dbg("Device %s not found.", device_path(device));
141                 return -EINVAL;
142         }
143
144         if (type == CRYPT_WIPE_DISK && S_ISBLK(st.st_mode)) {
145                 if (!crypt_dev_is_rotational(major(st.st_rdev),
146                                                 minor(st.st_rdev))) {
147                         type = CRYPT_WIPE_SSD;
148                         log_dbg("Non-rotational device, using SSD wipe mode.");
149                 } else
150                         log_dbg("Rotational device, using normal wipe mode.");
151         }
152
153         bsize = device_block_size(device);
154         if (bsize <= 0)
155                 return -EINVAL;
156
157         buffer = malloc(size);
158         if (!buffer)
159                 return -ENOMEM;
160
161         flags = O_RDWR;
162
163         /* use O_EXCL only for block devices */
164         if (exclusive && S_ISBLK(st.st_mode))
165                 flags |= O_EXCL;
166
167         /* coverity[toctou] */
168         devfd = device_open(device, flags);
169         if (devfd == -1) {
170                 free(buffer);
171                 return errno ? -errno : -EINVAL;
172         }
173
174         // FIXME: use fixed block size and loop here
175         switch (type) {
176                 case CRYPT_WIPE_ZERO:
177                         written = _crypt_wipe_zero(devfd, bsize, buffer, offset, size);
178                         break;
179                 case CRYPT_WIPE_DISK:
180                         written = _crypt_wipe_disk(devfd, bsize, buffer, offset, size);
181                         break;
182                 case CRYPT_WIPE_SSD:
183                         written = _crypt_wipe_ssd(devfd, bsize, buffer, offset, size);
184                         break;
185                 case CRYPT_WIPE_RANDOM:
186                         written = _crypt_wipe_random(devfd, bsize, buffer, offset, size);
187                         break;
188                 default:
189                         log_dbg("Unsuported wipe type requested: (%d)", type);
190                         written = -1;
191         }
192
193         close(devfd);
194         free(buffer);
195
196         if (written != (ssize_t)size || written < 0)
197                 return -EIO;
198
199         return 0;
200 }