utils_device_locking.c 8.96 KB
Newer Older
1 2 3
/*
 * Metadata on-disk locking for processes serialization
 *
Milan Broz's avatar
Milan Broz committed
4 5
 * Copyright (C) 2016-2019 Red Hat, Inc. All rights reserved.
 * Copyright (C) 2016-2019 Ondrej Kozina
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
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <errno.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h>     /* for major, minor */
#endif
#include <libgen.h>

#include "internal.h"
#include "utils_device_locking.h"

#define same_inode(buf1, buf2) \
	((buf1).st_ino == (buf2).st_ino && \
	 (buf1).st_dev == (buf2).st_dev)

#ifndef __GNUC__
# define __typeof__ typeof
#endif

enum lock_type {
	DEV_LOCK_READ = 0,
	DEV_LOCK_WRITE
};

struct crypt_lock_handle {
	dev_t devno;
	int flock_fd;
	enum lock_type type;
	__typeof__( ((struct stat*)0)->st_mode) mode;
};

static int resource_by_devno(char *res, size_t res_size, dev_t devno, unsigned fullpath)
{
	int r;

	if (fullpath)
		r = snprintf(res, res_size, "%s/L_%d:%d", DEFAULT_LUKS2_LOCK_PATH, major(devno), minor(devno));
	else
		r = snprintf(res, res_size, "L_%d:%d", major(devno), minor(devno));

	return (r < 0 || (size_t)r >= res_size) ? -EINVAL : 0;
}

static int open_lock_dir(struct crypt_device *cd, const char *dir, const char *base)
{
	int dirfd, lockdfd;

76
	dirfd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
77
	if (dirfd < 0) {
78
		log_dbg(cd, "Failed to open directory %s: (%d: %s).", dir, errno, strerror(errno));
79
		if (errno == ENOTDIR || errno == ENOENT)
80
			log_err(cd, _("Locking aborted. The locking path %s/%s is unusable (not a directory or missing)."), dir, base);
81 82 83 84 85 86 87 88 89 90
		return -EINVAL;
	}

	lockdfd = openat(dirfd, base, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
	if (lockdfd < 0) {
		if (errno == ENOENT) {
			log_std(cd, _("WARNING: Locking directory %s/%s is missing!\n"), dir, base);

			/* success or failure w/ errno == EEXIST either way just try to open the 'base' directory again */
			if (mkdirat(dirfd, base, DEFAULT_LUKS2_LOCK_DIR_PERMS) && errno != EEXIST)
91
				log_dbg(cd, "Failed to create directory %s in %s (%d: %s).", base, dir, errno, strerror(errno));
92 93
			else
				lockdfd = openat(dirfd, base, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
94
		} else {
95
			log_dbg(cd, "Failed to open directory %s/%s: (%d: %s)", dir, base, errno, strerror(errno));
96
			if (errno == ENOTDIR || errno == ELOOP)
97
				log_err(cd, _("Locking aborted. The locking path %s/%s is unusable (%s is not a directory)."), dir, base, base);
98
		}
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	}

	close(dirfd);
	return lockdfd >= 0 ? lockdfd : -EINVAL;
}

static int open_resource(struct crypt_device *cd, const char *res)
{
	int err, lockdir_fd, r;
	char dir[] = DEFAULT_LUKS2_LOCK_PATH,
	     base[] = DEFAULT_LUKS2_LOCK_PATH;

	lockdir_fd = open_lock_dir(cd, dirname(dir), basename(base));
	if (lockdir_fd < 0)
		return -EINVAL;

115
	log_dbg(cd, "Opening lock resource file %s/%s", DEFAULT_LUKS2_LOCK_PATH, res);
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
	r = openat(lockdir_fd, res, O_CREAT | O_NOFOLLOW | O_RDWR | O_CLOEXEC, 0777);
	err = errno;

	close(lockdir_fd);

	return r < 0 ? -err : r;
}

static int acquire_lock_handle(struct crypt_device *cd, const char *device_path, struct crypt_lock_handle *h)
{
	char res[PATH_MAX];
	int dev_fd, fd;
	struct stat st;

	dev_fd = open(device_path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
	if (dev_fd < 0)
		return -EINVAL;

	if (fstat(dev_fd, &st)) {
		close(dev_fd);
		return -EINVAL;
	}

	if (S_ISBLK(st.st_mode)) {
		if (resource_by_devno(res, sizeof(res), st.st_rdev, 0)) {
			close(dev_fd);
			return -EINVAL;
		}

		fd = open_resource(cd, res);
		close(dev_fd);
		if (fd < 0)
			return fd;

		h->flock_fd = fd;
		h->devno = st.st_rdev;
	} else if (S_ISREG(st.st_mode)) {
		// FIXME: workaround for nfsv4
		fd = open(device_path, O_RDWR | O_NONBLOCK | O_CLOEXEC);
		if (fd < 0)
			h->flock_fd = dev_fd;
		else {
			h->flock_fd = fd;
			close(dev_fd);
		}
	} else {
		/* Wrong device type */
		close(dev_fd);
		return -EINVAL;
	}

	h->mode = st.st_mode;

	return 0;
}

172
static void release_lock_handle(struct crypt_device *cd, struct crypt_lock_handle *h)
173 174 175 176 177 178 179
{
	char res[PATH_MAX];
	struct stat buf_a, buf_b;

	if (S_ISBLK(h->mode) && /* was it block device */
	    !flock(h->flock_fd, LOCK_EX | LOCK_NB) && /* lock to drop the file */
	    !resource_by_devno(res, sizeof(res), h->devno, 1) && /* acquire lock resource name */
Andrea Gelmini's avatar
Andrea Gelmini committed
180
	    !fstat(h->flock_fd, &buf_a) && /* read inode id referred by fd */
Rafael Fontenelle's avatar
Rafael Fontenelle committed
181
	    !stat(res, &buf_b) && /* does path file still exist? */
182 183
	    same_inode(buf_a, buf_b)) { /* is it same id as the one referenced by fd? */
		/* coverity[toctou] */
184
		if (unlink(res)) /* yes? unlink the file */
185
			log_dbg(cd, "Failed to unlink resource file: %s", res);
186
	}
187 188

	if (close(h->flock_fd))
189
		log_dbg(cd, "Failed to close resource fd (%d).", h->flock_fd);
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
}

int device_locked(struct crypt_lock_handle *h)
{
	return (h && (h->type == DEV_LOCK_READ || h->type == DEV_LOCK_WRITE));
}

int device_locked_readonly(struct crypt_lock_handle *h)
{
	return (h && h->type == DEV_LOCK_READ);
}

static int verify_lock_handle(const char *device_path, struct crypt_lock_handle *h)
{
	char res[PATH_MAX];
	struct stat lck_st, res_st;

	/* we locked a regular file, check during device_open() instead. No reason to check now */
	if (S_ISREG(h->mode))
		return 0;

	if (resource_by_devno(res, sizeof(res), h->devno, 1))
		return -EINVAL;

	if (fstat(h->flock_fd, &lck_st))
		return -EINVAL;

	return (stat(res, &res_st) || !same_inode(lck_st, res_st)) ? -EAGAIN : 0;
}

struct crypt_lock_handle *device_read_lock_handle(struct crypt_device *cd, const char *device_path)
{
	int r;
	struct crypt_lock_handle *h = malloc(sizeof(*h));

	if (!h)
		return NULL;

	do {
		r = acquire_lock_handle(cd, device_path, h);
		if (r)
			break;

233
		log_dbg(cd, "Acquiring read lock for device %s.", device_path);
234 235

		if (flock(h->flock_fd, LOCK_SH)) {
236
			log_dbg(cd, "Shared flock failed with errno %d.", errno);
237
			r = -EINVAL;
238
			release_lock_handle(cd, h);
239 240 241
			break;
		}

242
		log_dbg(cd, "Verifying read lock handle for device %s.", device_path);
243 244 245 246 247 248 249 250

		/*
		 * check whether another libcryptsetup process removed resource file before this
		 * one managed to flock() it. See release_lock_handle() for details
		 */
		r = verify_lock_handle(device_path, h);
		if (r) {
			flock(h->flock_fd, LOCK_UN);
251 252
			release_lock_handle(cd, h);
			log_dbg(cd, "Read lock handle verification failed.");
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
		}
	} while (r == -EAGAIN);

	if (r) {
		free(h);
		return NULL;
	}

	h->type = DEV_LOCK_READ;

	return h;
}

struct crypt_lock_handle *device_write_lock_handle(struct crypt_device *cd, const char *device_path)
{
	int r;
	struct crypt_lock_handle *h = malloc(sizeof(*h));

	if (!h)
		return NULL;

	do {
		r = acquire_lock_handle(cd, device_path, h);
		if (r)
			break;

279
		log_dbg(cd, "Acquiring write lock for device %s.", device_path);
280 281

		if (flock(h->flock_fd, LOCK_EX)) {
282
			log_dbg(cd, "Exclusive flock failed with errno %d.", errno);
283
			r = -EINVAL;
284
			release_lock_handle(cd, h);
285 286 287
			break;
		}

288
		log_dbg(cd, "Verifying write lock handle for device %s.", device_path);
289 290 291 292 293 294 295 296

		/*
		 * check whether another libcryptsetup process removed resource file before this
		 * one managed to flock() it. See release_lock_handle() for details
		 */
		r = verify_lock_handle(device_path, h);
		if (r) {
			flock(h->flock_fd, LOCK_UN);
297 298
			release_lock_handle(cd, h);
			log_dbg(cd, "Write lock handle verification failed.");
299 300 301 302 303 304 305 306 307 308 309 310 311
		}
	} while (r == -EAGAIN);

	if (r) {
		free(h);
		return NULL;
	}

	h->type = DEV_LOCK_WRITE;

	return h;
}

312
void device_unlock_handle(struct crypt_device *cd, struct crypt_lock_handle *h)
313 314
{
	if (flock(h->flock_fd, LOCK_UN))
315
		log_dbg(cd, "flock on fd %d failed.", h->flock_fd);
316

317
	release_lock_handle(cd, h);
318 319 320 321

	free(h);
}

322
int device_locked_verify(struct crypt_device *cd, int dev_fd, struct crypt_lock_handle *h)
323 324 325 326 327 328 329 330 331
{
	char res[PATH_MAX];
	struct stat dev_st, lck_st, st;

	if (fstat(dev_fd, &dev_st) || fstat(h->flock_fd, &lck_st))
		return 1;

	/* if device handle is regular file the handle must match the lock handle */
	if (S_ISREG(dev_st.st_mode)) {
332
		log_dbg(cd, "Veryfing locked device handle (regular file)");
333 334 335
		if (!same_inode(dev_st, lck_st))
			return 1;
	} else if (S_ISBLK(dev_st.st_mode)) {
336
		log_dbg(cd, "Veryfing locked device handle (bdev)");
337 338 339 340 341 342 343 344 345
		if (resource_by_devno(res, sizeof(res), dev_st.st_rdev, 1) ||
		    stat(res, &st) ||
		    !same_inode(lck_st, st))
			return 1;
	} else
		return 1;

	return 0;
}