]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: make id switching functions return bool stable-2.0
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 9 Apr 2019 17:59:01 +0000 (19:59 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 9 Apr 2019 18:00:13 +0000 (20:00 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/start.c
src/lxc/utils.c
src/lxc/utils.h

index 33585df7d33c0afe0a667dbf8c920e90104a7afd..ee91495702d475f044823f23e86f25df02a82c9b 100644 (file)
@@ -1146,15 +1146,13 @@ static int do_start(void *data)
                                  ? 0
                                  : handler->conf->init_gid;
 
-               ret = lxc_switch_uid_gid(nsuid, nsgid);
-               if (ret < 0)
+               if (!lxc_switch_uid_gid(nsuid, nsgid))
                        goto out_warn_father;
 
                /* Drop groups only after we switched to a valid gid in the new
                 * user namespace.
                 */
-               ret = lxc_setgroups(0, NULL);
-               if (ret < 0 && (handler->am_root || errno != EPERM))
+               if (!lxc_setgroups(0, NULL) && (handler->am_root || errno != EPERM))
                        goto out_warn_father;
 
                ret = prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
@@ -1349,12 +1347,11 @@ static int do_start(void *data)
        #else
        have_cap_setgid = false;
        #endif
-       if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) {
-               if (lxc_setgroups(0, NULL) < 0)
+       if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid)
+               if (!lxc_setgroups(0, NULL))
                        goto out_warn_father;
-       }
 
-       if (lxc_switch_uid_gid(new_uid, new_gid) < 0)
+       if (!lxc_switch_uid_gid(new_uid, new_gid))
                goto out_warn_father;
 
        ret = lxc_ambient_caps_down();
index e0f49bcb6bcea77b4ac62d388a7a62dbb6330b55..c0eb5e000217eead54cd6c6c5873d09434083232 100644 (file)
@@ -2059,33 +2059,41 @@ int lxc_safe_long_long(const char *numstr, long long int *converted)
        return 0;
 }
 
-int lxc_switch_uid_gid(uid_t uid, gid_t gid)
+bool lxc_switch_uid_gid(uid_t uid, gid_t gid)
 {
-       if (setgid(gid) < 0) {
-               SYSERROR("Failed to switch to gid %d.", gid);
-               return -errno;
+       int ret = 0;
+
+       if (gid != LXC_INVALID_GID) {
+               ret = setgid(gid);
+               if (ret < 0) {
+                       SYSERROR("Failed to switch to gid %d", gid);
+                       return false;
+               }
+               NOTICE("Switched to gid %d", gid);
        }
-       NOTICE("Switched to gid %d.", gid);
 
-       if (setuid(uid) < 0) {
-               SYSERROR("Failed to switch to uid %d.", uid);
-               return -errno;
+       if (uid != LXC_INVALID_UID) {
+               ret = setuid(uid);
+               if (ret < 0) {
+                       SYSERROR("Failed to switch to uid %d", uid);
+                       return false;
+               }
+               NOTICE("Switched to uid %d", uid);
        }
-       NOTICE("Switched to uid %d.", uid);
 
-       return 0;
+       return true;
 }
 
-/* Simple covenience function which enables uniform logging. */
-int lxc_setgroups(int size, gid_t list[])
+/* Simple convenience function which enables uniform logging. */
+bool lxc_setgroups(int size, gid_t list[])
 {
        if (setgroups(size, list) < 0) {
-               SYSERROR("Failed to setgroups().");
-               return -errno;
+               SYSERROR("Failed to setgroups()");
+               return false;
        }
-       NOTICE("Dropped additional groups.");
+       NOTICE("Dropped additional groups");
 
-       return 0;
+       return true;
 }
 
 static int lxc_get_unused_loop_dev_legacy(char *loop_name)
index 2af0bfd4120fefaa343c2da971a72c605c694f6c..074a2d522e3580f963bf50e9a9195a4a0e762eb4 100644 (file)
@@ -455,8 +455,8 @@ extern int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base);
 extern int parse_byte_size_string(const char *s, int64_t *converted);
 
 /* Switch to a new uid and gid. */
-int lxc_switch_uid_gid(uid_t uid, gid_t gid);
-int lxc_setgroups(int size, gid_t list[]);
+bool lxc_switch_uid_gid(uid_t uid, gid_t gid);
+bool lxc_setgroups(int size, gid_t list[]);
 
 /* Find an unused loop device and associate it with source. */
 int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);