~ubuntu-branches/ubuntu/maverick/cryptsetup/maverick-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <libdevmapper.h>

#include "libcryptsetup.h"
#include "internal.h"

#define DEVICE_DIR	"/dev"

#define	CRYPT_TARGET	"crypt"
#define	RETRY_COUNT	5

static void set_dm_error(int level, const char *file, int line,
                         const char *f, ...)
{
	va_list va;

	if (level > 3)
		return;

	va_start(va, f);
	set_error_va(f, va);
	va_end(va);
}

static int _dm_simple(int task, const char *name);

static int dm_init(void)
{
	dm_log_init(set_dm_error);
	if (!_dm_simple(DM_DEVICE_LIST_VERSIONS, "test")) {
		set_error("Cannot communicate with device-mapper. Is the dm_mod module loaded?");
		return -1;
	}

	return 1;	/* unsafe memory */
}

static void dm_exit(void)
{
	dm_log_init(NULL);
	dm_lib_release();
}

static char *__lookup_dev(char *path, dev_t dev)
{
	struct dirent *entry;
	struct stat st;
	char *ptr;
	char *result = NULL;
	DIR *dir;
	int space;

	path[PATH_MAX - 1] = '\0';
	ptr = path + strlen(path);
	*ptr++ = '/';
	*ptr = '\0';
	space = PATH_MAX - (ptr - path);

	dir = opendir(path);
	if (!dir)
		return NULL;

	while((entry = readdir(dir))) {
		if (entry->d_name[0] == '.' &&
		    (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
		                                  entry->d_name[2] == '\0')))
			continue;

		strncpy(ptr, entry->d_name, space);
		if (lstat(path, &st) < 0)
			continue;

		if (S_ISDIR(st.st_mode)) {
			result = __lookup_dev(path, dev);
			if (result)
				break;
		} else if (S_ISBLK(st.st_mode)) {
			if (st.st_rdev == dev) {
				result = strdup(path);
				break;
			}
		}
	}

	closedir(dir);

	return result;
}

static char *lookup_dev(const char *dev)
{
	uint32_t major, minor;
	char buf[PATH_MAX + 1];

	if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
		return NULL;

	strncpy(buf, DEVICE_DIR, PATH_MAX);
	buf[PATH_MAX] = '\0';

	return __lookup_dev(buf, makedev(major, minor));
}

static char *get_params(struct crypt_options *options, const char *key)
{
	char *params;
	char *hexkey;
	int i;

	hexkey = safe_alloc(options->key_size * 2 + 1);
	if (!hexkey) {
		set_error("Memory allocation problem");
		return NULL;
	}

	for(i = 0; i < options->key_size; i++)
		sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);

	params = safe_alloc(strlen(hexkey) + strlen(options->cipher) +
	                    strlen(options->device) + 64);
	if (!params) {
		set_error("Memory allocation problem");
		goto out;
	}

	sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
	        options->cipher, hexkey, options->skip,
	        options->device, options->offset);

out:
	safe_free(hexkey);

	return params;
}

/* DM helpers */
static int _dm_simple(int task, const char *name)
{
	int r = 0;
	struct dm_task *dmt;

	if (!(dmt = dm_task_create(task)))
		return 0;

	if (!dm_task_set_name(dmt, name))
		goto out;

	r = dm_task_run(dmt);

      out:
	dm_task_destroy(dmt);
	return r;
}

static int _error_device(struct crypt_options *options)
{
	struct dm_task *dmt;
	int r = 0;

	if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
		return 0;

	if (!dm_task_set_name(dmt, options->name))
		goto error;

	if (!dm_task_add_target(dmt, UINT64_C(0), options->size, "error", ""))
		goto error;

	if (!dm_task_set_ro(dmt))
		goto error;

	if (!dm_task_no_open_count(dmt))
		goto error;

	if (!dm_task_run(dmt))
		goto error;

	if (!_dm_simple(DM_DEVICE_RESUME, options->name)) {
		_dm_simple(DM_DEVICE_CLEAR, options->name);
		goto error;
	}

	r = 1;

error:
	dm_task_destroy(dmt);
	return r;
}

static int _dm_remove(struct crypt_options *options, int force)
{
	int r = -EINVAL;
	int retries = force ? RETRY_COUNT : 1;

	/* If force flag is set, replace device with error, read-only target.
	 * it should stop processes from reading it and also removed underlying
	 * device from mapping, so it is usable again.
	 * Force flag should be used only for temporary devices, which are
	 * intended to work inside cryptsetup only!
	 * Anyway, if some process try to read temporary cryptsetup device,
	 * it is bug - no other process should try touch it (e.g. udev).
	 */
	if (force) {
		 _error_device(options);
		retries = RETRY_COUNT;
	}

	do {
		r = _dm_simple(DM_DEVICE_REMOVE, options->name) ? 0 : -EINVAL;
		if (--retries)
			sleep(1);
	} while (r == -EINVAL && retries);

	dm_task_update_nodes();

	return r;
}

static int dm_create_device(int reload, struct crypt_options *options,
                            const char *key)
{
	struct dm_task *dmt = NULL;
	struct dm_task *dmt_query = NULL;
	struct dm_info dmi;
	char *params = NULL;
	int r = -EINVAL;

	params = get_params(options, key);
	if (!params)
		goto out_no_removal;
	if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
	                                  : DM_DEVICE_CREATE)))
		goto out;
	if (!dm_task_set_name(dmt, options->name))
		goto out;
	if (options->flags & CRYPT_FLAG_READONLY && !dm_task_set_ro(dmt))
                goto out;
	if (!dm_task_add_target(dmt, 0, options->size, CRYPT_TARGET, params))
		goto out;
	if (!dm_task_run(dmt))
		goto out;

	if (reload) {
		dm_task_destroy(dmt);
		if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
			goto out;
		if (!dm_task_set_name(dmt, options->name))
			goto out;
		if (!dm_task_run(dmt))
			goto out;
	}

	if (!dm_task_get_info(dmt, &dmi))
		goto out;
	if (dmi.read_only)
		options->flags |= CRYPT_FLAG_READONLY;

	r = 0;
out:
	if (r < 0 && !reload) {
		char *error = (char *)get_error();
		if (error)
			error = strdup(error);

		if (!_dm_remove(options, 0))
			goto out_restore_error;

out_restore_error:
		set_error("%s", error);
		if (error)
			free(error);
	}

out_no_removal:
	if (params)
		safe_free(params);
	if (dmt)
		dm_task_destroy(dmt);
	if(dmt_query)
		dm_task_destroy(dmt_query);
	dm_task_update_nodes();
	return r;
}

static int dm_query_device(int details, struct crypt_options *options,
                           char **key)
{
	struct dm_task *dmt;
	struct dm_info dmi;
	uint64_t start, length;
	char *target_type, *params;
	void *next = NULL;
	int r = -EINVAL;

	if (!(dmt = dm_task_create(details ? DM_DEVICE_TABLE
	                                   : DM_DEVICE_STATUS)))
		goto out;
	if (!dm_task_set_name(dmt, options->name))
		goto out;
	r = -ENODEV;
	if (!dm_task_run(dmt))
		goto out;

	r = -EINVAL;
	if (!dm_task_get_info(dmt, &dmi))
		goto out;

	if (!dmi.exists) {
		r = -ENODEV;
		goto out;
	}

	next = dm_get_next_target(dmt, next, &start, &length,
	                          &target_type, &params);
	if (!target_type || strcmp(target_type, CRYPT_TARGET) != 0 ||
	    start != 0 || next)
		goto out;

	options->hash = NULL;
	options->cipher = NULL;
	options->offset = 0;
	options->skip = 0;
	options->size = length;
	if (details) {
		char *cipher, *key_, *device;
		uint64_t val64;

		set_error("Invalid dm table");

		cipher = strsep(&params, " ");
		key_ = strsep(&params, " ");
		if (!params)
			goto out;

		val64 = strtoull(params, &params, 10);
		if (*params != ' ')
			goto out;
		params++;
		options->skip = val64;

		device = strsep(&params, " ");
		if (!params)
			goto out;

		val64 = strtoull(params, &params, 10);
		if (*params)
			goto out;
		options->offset = val64;

		options->cipher = strdup(cipher);
		options->key_size = strlen(key_) / 2;
		if (key) {
			char buffer[3];
			char *endp;
			int i;

			*key = safe_alloc(options->key_size);
			if (!*key) {
				set_error("Out of memory");
				r = -ENOMEM;
				goto out;
			}

			buffer[2] = '\0';
			for(i = 0; i < options->key_size; i++) {
				memcpy(buffer, &key_[i * 2], 2);
				(*key)[i] = strtoul(buffer, &endp, 16);
				if (endp != &buffer[2]) {
					safe_free(key);
					*key = NULL;
					goto out;
				}
			}
		}
		memset(key_, 0, strlen(key_));
		options->device = lookup_dev(device);

		set_error(NULL);
	}

	r = (dmi.open_count > 0);

out:
	if (dmt)
		dm_task_destroy(dmt);
	if (r >= 0) {
		if (options->device)
			options->flags |= CRYPT_FLAG_FREE_DEVICE;
		if (options->cipher)
			options->flags |= CRYPT_FLAG_FREE_CIPHER;
		options->flags &= ~CRYPT_FLAG_READONLY;
		if (dmi.read_only)
			options->flags |= CRYPT_FLAG_READONLY;
	} else {
		if (options->device) {
			free((char *)options->device);
			options->device = NULL;
			options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
		}
		if (options->cipher) {
			free((char *)options->cipher);
			options->cipher = NULL;
			options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
		}
	}
	return r;
}

static int dm_remove_device(int force, struct crypt_options *options)
{
	if (!options || !options->name)
		return -EINVAL;

	return _dm_remove(options, force);;
}


static const char *dm_get_dir(void)
{
	return dm_dir();
}

struct setup_backend setup_libdevmapper_backend = {
	.name = "dm-crypt",
	.init = dm_init,
	.exit = dm_exit,
	.create = dm_create_device,
	.status = dm_query_device,
	.remove = dm_remove_device,
	.dir = dm_get_dir
};