]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: improve lxc_switch_uid_gid()
authorChristian Brauner <christian.brauner@ubuntu.com>
Sun, 9 Sep 2018 11:11:21 +0000 (13:11 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 10 Sep 2018 22:44:57 +0000 (00:44 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/utils.c
src/lxc/utils.h

index b85383a42ae78ef33f3470aca170b2c38cfca510..ff02bba968014083582027dabecba77c6ba53e23 100644 (file)
@@ -1353,19 +1353,27 @@ int lxc_preserve_ns(const int pid, const char *ns)
 
 int 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 -1;
+               }
+               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 -1;
+               }
+               NOTICE("Switched to uid %d", uid);
        }
-       NOTICE("Switched to uid %d.", uid);
 
-       return 0;
+       return ret;
 }
 
 /* Simple covenience function which enables uniform logging. */
index 51cfe4c85f03a4c34dadda0ee5ff546190694e03..947b15e16d63d4fc35bfa4b89e28d3a214721c47 100644 (file)
@@ -358,7 +358,9 @@ extern int lxc_preserve_ns(const int pid, const char *ns);
 /* Check whether a signal is blocked by a process. */
 extern bool task_blocks_signal(pid_t pid, int signal);
 
-/* Switch to a new uid and gid. */
+/* Switch to a new uid and gid.
+ * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
+ */
 extern int lxc_switch_uid_gid(uid_t uid, gid_t gid);
 extern int lxc_setgroups(int size, gid_t list[]);