Fix race while allocating free loop device.

Fixes Issue #314.

Thanks to Martin Jeřábek for the report.
This commit is contained in:
Milan Broz
2017-01-20 15:27:40 +01:00
parent a77c609c62
commit ef7ecb5567
4 changed files with 48 additions and 59 deletions

View File

@@ -419,25 +419,22 @@ out:
static int device_internal_prepare(struct crypt_device *cd, struct device *device) static int device_internal_prepare(struct crypt_device *cd, struct device *device)
{ {
char *loop_device, *file_path = NULL; char *loop_device = NULL, *file_path = NULL;
int r, loop_fd, readonly = 0; int r, loop_fd, readonly = 0;
if (device->init_done) if (device->init_done)
return 0; return 0;
log_dbg("Allocating a free loop device."); if (getuid() || geteuid()) {
loop_device = crypt_loop_get_device(); log_err(cd, _("Cannot use a loopback device, "
if (!loop_device) { "running as non-root user.\n"));
if (getuid() || geteuid())
log_err(cd, _("Cannot use a loopback device, "
"running as non-root user.\n"));
else
log_err(cd, _("Cannot find a free loopback device.\n"));
return -ENOTSUP; return -ENOTSUP;
} }
log_dbg("Allocating a free loop device.");
/* Keep the loop open, dettached on last close. */ /* Keep the loop open, dettached on last close. */
loop_fd = crypt_loop_attach(loop_device, device->path, 0, 1, &readonly); loop_fd = crypt_loop_attach(&loop_device, device->path, 0, 1, &readonly);
if (loop_fd == -1) { if (loop_fd == -1) {
log_err(cd, _("Attaching loopback device failed " log_err(cd, _("Attaching loopback device failed "
"(loop device with autoclear flag is required).\n")); "(loop device with autoclear flag is required).\n"));

View File

@@ -19,6 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@@ -73,7 +74,7 @@ static char *crypt_loop_get_device_old(void)
return NULL; return NULL;
} }
char *crypt_loop_get_device(void) static char *crypt_loop_get_device(void)
{ {
char dev[64]; char dev[64];
int i, loop_fd; int i, loop_fd;
@@ -99,13 +100,15 @@ char *crypt_loop_get_device(void)
return strdup(dev); return strdup(dev);
} }
int crypt_loop_attach(const char *loop, const char *file, int offset, int crypt_loop_attach(char **loop, const char *file, int offset,
int autoclear, int *readonly) int autoclear, int *readonly)
{ {
struct loop_info64 lo64 = {0}; struct loop_info64 lo64 = {0};
char *lo_file_name; char *lo_file_name;
int loop_fd = -1, file_fd = -1, r = 1; int loop_fd = -1, file_fd = -1, r = 1;
*loop = NULL;
file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL); file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) { if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
*readonly = 1; *readonly = 1;
@@ -114,9 +117,25 @@ int crypt_loop_attach(const char *loop, const char *file, int offset,
if (file_fd < 0) if (file_fd < 0)
goto out; goto out;
loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR); while (loop_fd < 0) {
if (loop_fd < 0) *loop = crypt_loop_get_device();
goto out; if (!*loop)
goto out;
loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
if (loop_fd < 0)
goto out;
if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
if (errno != EBUSY)
goto out;
free(*loop);
*loop = NULL;
close(loop_fd);
loop_fd = -1;
}
}
lo_file_name = (char*)lo64.lo_file_name; lo_file_name = (char*)lo64.lo_file_name;
lo_file_name[LO_NAME_SIZE-1] = '\0'; lo_file_name[LO_NAME_SIZE-1] = '\0';
@@ -125,9 +144,6 @@ int crypt_loop_attach(const char *loop, const char *file, int offset,
if (autoclear) if (autoclear)
lo64.lo_flags |= LO_FLAGS_AUTOCLEAR; lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
goto out;
if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) { if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
(void)ioctl(loop_fd, LOOP_CLR_FD, 0); (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
goto out; goto out;
@@ -149,6 +165,10 @@ out:
close(loop_fd); close(loop_fd);
if (file_fd >= 0) if (file_fd >= 0)
close(file_fd); close(file_fd);
if (r && *loop) {
free(*loop);
*loop = NULL;
}
return r ? -1 : loop_fd; return r ? -1 : loop_fd;
} }

View File

@@ -24,10 +24,9 @@
/* loopback device helpers */ /* loopback device helpers */
char *crypt_loop_get_device(void);
char *crypt_loop_backing_file(const char *loop); char *crypt_loop_backing_file(const char *loop);
int crypt_loop_device(const char *loop); int crypt_loop_device(const char *loop);
int crypt_loop_attach(const char *loop, const char *file, int offset, int crypt_loop_attach(char **loop, const char *file, int offset,
int autoclear, int *readonly); int autoclear, int *readonly);
int crypt_loop_detach(const char *loop); int crypt_loop_detach(const char *loop);
int crypt_loop_resize(const char *loop); int crypt_loop_resize(const char *loop);

View File

@@ -423,16 +423,8 @@ static int _setup(void)
if (_system(cmd, 1)) if (_system(cmd, 1))
return 1; return 1;
if (!THE_LOOP_DEV) fd = crypt_loop_attach(&THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
THE_LOOP_DEV = crypt_loop_get_device(); close(fd);
if (!THE_LOOP_DEV) {
printf("Cannot find free loop device.\n");
return 1;
}
if (crypt_loop_device(THE_LOOP_DEV)) {
fd = crypt_loop_attach(THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
close(fd);
}
tmp_file_1 = strdup(THE_LFILE_TEMPLATE); tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
if ((fd=mkstemp(tmp_file_1)) == -1) { if ((fd=mkstemp(tmp_file_1)) == -1) {
@@ -447,34 +439,15 @@ static int _setup(void)
_system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1); _system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
_system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1); _system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
if (!DEVICE_1)
DEVICE_1 = crypt_loop_get_device(); _system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
if (!DEVICE_1) { fd = crypt_loop_attach(&DEVICE_1, IMAGE1, 0, 0, &ro);
printf("Cannot find free loop device.\n"); close(fd);
return 1;
} _system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4 2>/dev/null", 1);
if (crypt_loop_device(DEVICE_1)) { fd = crypt_loop_attach(&DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
_system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1); close(fd);
fd = crypt_loop_attach(DEVICE_1, IMAGE1, 0, 0, &ro);
close(fd);
}
if (!DEVICE_2)
DEVICE_2 = crypt_loop_get_device();
if (!DEVICE_2) {
printf("Cannot find free loop device.\n");
return 1;
}
if (crypt_loop_device(DEVICE_2)) {
_system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4 2>/dev/null", 1);
fd = crypt_loop_attach(DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
close(fd);
}
if (!DEVICE_3)
DEVICE_3 = crypt_loop_get_device();
if (!DEVICE_3) {
printf("Cannot find free loop device.\n");
return 1;
}
/* Keymaterial offset is less than 8 sectors */ /* Keymaterial offset is less than 8 sectors */
_system(" [ ! -e " EVL_HEADER_1 " ] && bzip2 -dk " EVL_HEADER_1 ".bz2", 1); _system(" [ ! -e " EVL_HEADER_1 " ] && bzip2 -dk " EVL_HEADER_1 ".bz2", 1);
/* keymaterial offset aims into payload area */ /* keymaterial offset aims into payload area */
@@ -1486,7 +1459,7 @@ static void LuksHeaderBackup(void)
crypt_free(cd); crypt_free(cd);
// exercise luksOpen using backup header on block device // exercise luksOpen using backup header on block device
fd = crypt_loop_attach(DEVICE_3, BACKUP_FILE, 0, 0, &ro); fd = crypt_loop_attach(&DEVICE_3, BACKUP_FILE, 0, 0, &ro);
close(fd); close(fd);
OK_(fd < 0); OK_(fd < 0);
OK_(crypt_init(&cd, DEVICE_3)); OK_(crypt_init(&cd, DEVICE_3));