|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
From 56fe0eb7f350b8c75fd952ce930a9ef0f00d45fc Mon Sep 17 00:00:00 2001
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
Date: Fri, 20 Oct 2017 12:41:04 +0200
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
Subject: [PATCH] core/dynamic-user: use _cleanup_ in dynamic user locking
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
This makes the code a bit easier to read.
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
(cherry picked from commit 362d90b7f2776f7fabf78274587fd38ceb1b1c12)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
---
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
src/core/dynamic-user.c | 78 +++++++++++++++++++++++++++----------------------
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
1 file changed, 43 insertions(+), 35 deletions(-)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
index 8f229d27ff..0f4ec15721 100644
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
--- a/src/core/dynamic-user.c
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+++ b/src/core/dynamic-user.c
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -421,9 +421,25 @@ static void unlink_uid_lock(int lock_fd, uid_t uid, const char *name) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
(void) make_uid_symlinks(uid, name, false); /* remove direct lookup symlinks */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+static int lockfp(int fd, int *fd_lock) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ if (lockf(fd, F_LOCK, 0) < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ *fd_lock = fd;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+static void unlockfp(int *fd_lock) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ if (*fd_lock < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ lockf(*fd_lock, F_ULOCK, 0);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ *fd_lock = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
static int dynamic_user_realize(DynamicUser *d, char **suggested_dirs, uid_t *ret, bool is_user) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- _cleanup_close_ int etc_passwd_lock_fd = -1, uid_lock_fd = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ _cleanup_(unlockfp) int storage_socket0_lock = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ _cleanup_close_ int uid_lock_fd = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ _cleanup_close_ int etc_passwd_lock_fd = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
uid_t uid = UID_INVALID;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
int r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -432,8 +448,9 @@ static int dynamic_user_realize(DynamicUser *d, char **suggested_dirs, uid_t *re
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* Acquire a UID for the user name. This will allocate a UID for the user name if the user doesn't exist
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
* yet. If it already exists its existing UID/GID will be reused. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- if (lockf(d->storage_socket[0], F_LOCK, 0) < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ r = lockfp(d->storage_socket[0], &storage_socket0_lock);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ if (r < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
r = dynamic_user_pop(d, &uid, &uid_lock_fd);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
if (r < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -441,11 +458,11 @@ static int dynamic_user_realize(DynamicUser *d, char **suggested_dirs, uid_t *re
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
uid_t new_uid;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
if (r != -EAGAIN)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- goto finish;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* OK, nothing stored yet, let's try to find something useful. While we are working on this release the
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
* lock however, so that nobody else blocks on our NSS lookups. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- (void) lockf(d->storage_socket[0], F_ULOCK, 0);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ unlockfp(&storage_socket0_lock);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* Let's see if a proper, static user or group by this name exists. Try to take the lock on
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
* /etc/passwd, if that fails with EROFS then /etc is read-only. In that case it's fine if we don't
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -494,9 +511,10 @@ static int dynamic_user_realize(DynamicUser *d, char **suggested_dirs, uid_t *re
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* So, we found a working UID/lock combination. Let's see if we actually still need it. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- if (lockf(d->storage_socket[0], F_LOCK, 0) < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ r = lockfp(d->storage_socket[0], &storage_socket0_lock);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ if (r < 0) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
unlink_uid_lock(uid_lock_fd, uid, d->name);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
r = dynamic_user_pop(d, &new_uid, &new_uid_lock_fd);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -504,7 +522,7 @@ static int dynamic_user_realize(DynamicUser *d, char **suggested_dirs, uid_t *re
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
if (r != -EAGAIN) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* OK, something bad happened, let's get rid of the bits we acquired. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
unlink_uid_lock(uid_lock_fd, uid, d->name);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- goto finish;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* Great! Nothing is stored here, still. Store our newly acquired data. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -525,17 +543,14 @@ static int dynamic_user_realize(DynamicUser *d, char **suggested_dirs, uid_t *re
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
* dynamically right here, push that in along with the lock fd for it. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
r = dynamic_user_push(d, uid, uid_lock_fd);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
if (r < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- goto finish;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
*ret = uid;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- r = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
-finish:
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- (void) lockf(d->storage_socket[0], F_ULOCK, 0);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
static int dynamic_user_current(DynamicUser *d, uid_t *ret) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ _cleanup_(unlockfp) int storage_socket0_lock = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
_cleanup_close_ int lock_fd = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
uid_t uid;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
int r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -545,23 +560,20 @@ static int dynamic_user_current(DynamicUser *d, uid_t *ret) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* Get the currently assigned UID for the user, if there's any. This simply pops the data from the storage socket, and pushes it back in right-away. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- if (lockf(d->storage_socket[0], F_LOCK, 0) < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ r = lockfp(d->storage_socket[0], &storage_socket0_lock);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ if (r < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
r = dynamic_user_pop(d, &uid, &lock_fd);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
if (r < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- goto finish;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
r = dynamic_user_push(d, uid, lock_fd);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
if (r < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- goto finish;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
*ret = uid;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- r = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
-finish:
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- (void) lockf(d->storage_socket[0], F_ULOCK, 0);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
static DynamicUser* dynamic_user_ref(DynamicUser *d) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -589,6 +601,7 @@ static DynamicUser* dynamic_user_unref(DynamicUser *d) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
static int dynamic_user_close(DynamicUser *d) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ _cleanup_(unlockfp) int storage_socket0_lock = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
_cleanup_close_ int lock_fd = -1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
uid_t uid;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
int r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
@@ -596,25 +609,20 @@ static int dynamic_user_close(DynamicUser *d) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* Release the user ID, by releasing the lock on it, and emptying the storage socket. After this the user is
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
* unrealized again, much like it was after it the DynamicUser object was first allocated. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- if (lockf(d->storage_socket[0], F_LOCK, 0) < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- return -errno;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ r = lockfp(d->storage_socket[0], &storage_socket0_lock);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ if (r < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
r = dynamic_user_pop(d, &uid, &lock_fd);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- if (r == -EAGAIN) {
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ if (r == -EAGAIN)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* User wasn't realized yet, nothing to do. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- r = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- goto finish;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- }
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return 0;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
if (r < 0)
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- goto finish;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
/* This dynamic user was realized and dynamically allocated. In this case, let's remove the lock file. */
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
unlink_uid_lock(lock_fd, uid, d->name);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- r = 1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
-finish:
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- (void) lockf(d->storage_socket[0], F_ULOCK, 0);
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
- return r;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
+ return 1;
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
|
|
Zbigniew Jędrzejewski-Szmek |
35bb94 |
static DynamicUser* dynamic_user_destroy(DynamicUser *d) {
|