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