]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Remove (nearly) all use of getuid()/getgid()
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 9 Oct 2013 11:13:45 +0000 (12:13 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 29 Oct 2013 16:19:00 +0000 (16:19 +0000)
Most of the usage of getuid()/getgid() is in cases where we are
considering what privileges we have. As such the code should be
using the effective IDs, not real IDs.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
(cherry picked from commit 9b0af09240618184fea5884952941217e65b824f)

16 files changed:
src/libvirt.c
src/locking/lock_daemon.c
src/locking/lock_driver_lockd.c
src/lxc/lxc_controller.c
src/qemu/qemu_driver.c
src/remote/remote_driver.c
src/storage/storage_backend.c
src/storage/storage_backend_fs.c
src/storage/storage_backend_logical.c
src/util/virfile.c
src/util/viridentity.c
src/util/virstoragefile.c
src/vbox/vbox_driver.c
src/vbox/vbox_tmpl.c
tests/qemumonitortestutils.c
tests/virnetsockettest.c

index aec5d80fa4b746b5592da7e8e220f63ae1a8ba78..96d8fdca39377b722606ff03f8e8649312a4a3c4 100644 (file)
@@ -133,7 +133,7 @@ static int virConnectAuthGainPolkit(const char *privilege) {
     int status;
     int ret = -1;
 
-    if (getuid() == 0)
+    if (geteuid() == 0)
         return 0;
 
     cmd = virCommandNewArgList(POLKIT_AUTH, "--obtain", privilege, NULL);
index cef5bd66666fc97deed02fdb56bc46989a200ce5..35ccb4e9cb57bbf64d4b33b8960aa7e5f0c42fe6 100644 (file)
@@ -1167,7 +1167,7 @@ int main(int argc, char **argv) {
         {0, 0, 0, 0}
     };
 
-    privileged = getuid() == 0;
+    privileged = geteuid() == 0;
 
     if (setlocale(LC_ALL, "") == NULL ||
         bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
index 86ce2d87be9a78cc23e366a24e0e5e7f39f32ef0..f3b9467fe5953991426967a2582d3a3cccce7715 100644 (file)
@@ -302,7 +302,7 @@ virLockManagerLockDaemonConnect(virLockManagerPtr lock,
 {
     virNetClientPtr client;
 
-    if (!(client = virLockManagerLockDaemonConnectionNew(getuid() == 0, program)))
+    if (!(client = virLockManagerLockDaemonConnectionNew(geteuid() == 0, program)))
         return NULL;
 
     if (virLockManagerLockDaemonConnectionRegister(lock,
@@ -331,7 +331,7 @@ static int virLockManagerLockDaemonSetupLockspace(const char *path)
     memset(&args, 0, sizeof(args));
     args.path = (char*)path;
 
-    if (!(client = virLockManagerLockDaemonConnectionNew(getuid() == 0, &program)))
+    if (!(client = virLockManagerLockDaemonConnectionNew(geteuid() == 0, &program)))
         return -1;
 
     if (virNetClientProgramCall(program,
index c7dffd7a761ba003ec8258c42f9b0c318415f5f1..69044d3aa32319e16b91f97105059461aa8a8165 100644 (file)
@@ -2346,7 +2346,7 @@ int main(int argc, char *argv[])
         goto cleanup;
     }
 
-    if (getuid() != 0) {
+    if (geteuid() != 0) {
         fprintf(stderr, "%s: must be run as the 'root' user\n", argv[0]);
         goto cleanup;
     }
index 068d29ff626f1051b4582637d883b3abb1ebf5c0..5b594a849a222abf40ee1bae0ee9452d8c281b2b 100644 (file)
@@ -2787,8 +2787,8 @@ qemuOpenFileAs(uid_t fallback_uid, gid_t fallback_gid,
     unsigned int vfoflags = 0;
     int fd = -1;
     int path_shared = virStorageFileIsSharedFS(path);
-    uid_t uid = getuid();
-    gid_t gid = getgid();
+    uid_t uid = geteuid();
+    gid_t gid = getegid();
 
     /* path might be a pre-existing block dev, in which case
      * we need to skip the create step, and also avoid unlink
@@ -2828,7 +2828,7 @@ qemuOpenFileAs(uid_t fallback_uid, gid_t fallback_gid,
                qemu user is non-root, just set a flag to
                bypass security driver shenanigans, and retry the operation
                after doing setuid to qemu user */
-            if ((fd != -EACCES && fd != -EPERM) || fallback_uid == getuid())
+            if ((fd != -EACCES && fd != -EPERM) || fallback_uid == geteuid())
                 goto error;
 
             /* On Linux we can also verify the FS-type of the directory. */
index 4f2726926541dd6690bc7c0a38808e5ba95b2d0c..cbeba5191dbb3d3e97cc4ad4725414d534c3f4b4 100644 (file)
@@ -995,7 +995,7 @@ remoteConnectOpen(virConnectPtr conn,
          (strstr(conn->uri->scheme, "+unix") != NULL)) &&
         (STREQ(conn->uri->path, "/session") ||
          STRPREFIX(conn->uri->scheme, "test+")) &&
-        getuid() > 0) {
+        geteuid() > 0) {
         VIR_DEBUG("Auto-spawn user daemon instance");
         rflags |= VIR_DRV_OPEN_REMOTE_USER;
         if (!virIsSUID() &&
@@ -1013,7 +1013,7 @@ remoteConnectOpen(virConnectPtr conn,
     if (!conn->uri) {
         VIR_DEBUG("Auto-probe remote URI");
 #ifndef __sun
-        if (getuid() > 0) {
+        if (geteuid() > 0) {
             VIR_DEBUG("Auto-spawn user daemon instance");
             rflags |= VIR_DRV_OPEN_REMOTE_USER;
             if (!virIsSUID() &&
index b7edf859500403c4ed730396851b5cffc2b6c475..4b4857968b6444e9221d180ed7a7f04e1e946007 100644 (file)
@@ -527,11 +527,11 @@ virStorageBackendCreateExecCommand(virStoragePoolObjPtr pool,
     bool filecreated = false;
 
     if ((pool->def->type == VIR_STORAGE_POOL_NETFS)
-        && (((getuid() == 0)
+        && (((geteuid() == 0)
              && (vol->target.perms.uid != (uid_t) -1)
              && (vol->target.perms.uid != 0))
             || ((vol->target.perms.gid != (gid_t) -1)
-                && (vol->target.perms.gid != getgid())))) {
+                && (vol->target.perms.gid != getegid())))) {
 
         virCommandSetUID(cmd, vol->target.perms.uid);
         virCommandSetGID(cmd, vol->target.perms.gid);
index f9b0326561c32954781bff7f08890bc37fb20de9..7ff950b12e60b6a1f7c3ab0518a22b1e9e13733f 100644 (file)
@@ -785,9 +785,9 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
 
     /* Reflect the actual uid and gid to the config. */
     if (pool->def->target.perms.uid == (uid_t) -1)
-        pool->def->target.perms.uid = getuid();
+        pool->def->target.perms.uid = geteuid();
     if (pool->def->target.perms.gid == (gid_t) -1)
-        pool->def->target.perms.gid = getgid();
+        pool->def->target.perms.gid = getegid();
 
     if (flags != 0) {
         ret = virStorageBackendMakeFileSystem(pool, flags);
index a1a37a1428470263b6b88c9b618834111eb69ed2..c720d1aab6bedf7726d804896d816dff75b7da05 100644 (file)
@@ -718,7 +718,7 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
         goto error;
 
     /* We can only chown/grp if root */
-    if (getuid() == 0) {
+    if (geteuid() == 0) {
         if (fchown(fd, vol->target.perms.uid, vol->target.perms.gid) < 0) {
             virReportSystemError(errno,
                                  _("cannot set file owner '%s'"),
index 0db3967ad67fae0a464411de7d64993996b24fcf..8cff3c9d413a2952f6fea79bb99c1615b72ab3cb 100644 (file)
@@ -1539,8 +1539,8 @@ virFileAccessibleAs(const char *path, int mode,
     gid_t *groups;
     int ngroups;
 
-    if (uid == getuid() &&
-        gid == getgid())
+    if (uid == geteuid() &&
+        gid == getegid())
         return access(path, mode);
 
     ngroups = virGetGroupList(uid, gid, &groups);
@@ -1832,9 +1832,9 @@ virFileOpenAs(const char *path, int openflags, mode_t mode,
 
     /* allow using -1 to mean "current value" */
     if (uid == (uid_t) -1)
-        uid = getuid();
+        uid = geteuid();
     if (gid == (gid_t) -1)
-        gid = getgid();
+        gid = getegid();
 
     /* treat absence of both flags as presence of both for simpler
      * calling. */
@@ -1842,7 +1842,7 @@ virFileOpenAs(const char *path, int openflags, mode_t mode,
         flags |= VIR_FILE_OPEN_NOFORK|VIR_FILE_OPEN_FORK;
 
     if ((flags & VIR_FILE_OPEN_NOFORK)
-        || (getuid() != 0)
+        || (geteuid() != 0)
         || ((uid == 0) && (gid == 0))) {
 
         if ((fd = open(path, openflags, mode)) < 0) {
@@ -1953,12 +1953,12 @@ virDirCreate(const char *path,
 
     /* allow using -1 to mean "current value" */
     if (uid == (uid_t) -1)
-        uid = getuid();
+        uid = geteuid();
     if (gid == (gid_t) -1)
-        gid = getgid();
+        gid = getegid();
 
     if ((!(flags & VIR_DIR_CREATE_AS_UID))
-        || (getuid() != 0)
+        || (geteuid() != 0)
         || ((uid == 0) && (gid == 0))
         || ((flags & VIR_DIR_CREATE_ALLOW_EXIST) && (stat(path, &st) >= 0))) {
         return virDirCreateNoFork(path, mode, uid, gid, flags);
index f681f85553f6448c2dbff51c9942c2c9143a0c14..4f5127cde7a712ae41eb4381efbda6b5e9e2befe 100644 (file)
@@ -157,14 +157,14 @@ virIdentityPtr virIdentityGetSystem(void)
         virAsprintf(&processtime, "%llu", timestamp) < 0)
         goto cleanup;
 
-    if (!(username = virGetUserName(getuid())))
+    if (!(username = virGetUserName(geteuid())))
         goto cleanup;
-    if (virAsprintf(&userid, "%d", (int)getuid()) < 0)
+    if (virAsprintf(&userid, "%d", (int)geteuid()) < 0)
         goto cleanup;
 
-    if (!(groupname = virGetGroupName(getgid())))
+    if (!(groupname = virGetGroupName(getegid())))
         goto cleanup;
-    if (virAsprintf(&groupid, "%d", (int)getgid()) < 0)
+    if (virAsprintf(&groupid, "%d", (int)getegid()) < 0)
         goto cleanup;
 
 #if WITH_SELINUX
index 0b9cec3b048decf8c1f9d2156350b1e00e22caad..945359931defd92ac54385b5d42c8d255d1b555b 100644 (file)
@@ -572,7 +572,7 @@ virFindBackingFile(const char *start, bool start_is_dir, const char *path,
         goto cleanup;
     }
 
-    if (virFileAccessibleAs(combined, F_OK, getuid(), getgid()) < 0) {
+    if (virFileAccessibleAs(combined, F_OK, geteuid(), getegid()) < 0) {
         virReportSystemError(errno,
                              _("Cannot access backing file '%s'"),
                              combined);
index 9d075741f9dd8d8209fafe2b704a06ecaae77fd6..4978913ca8c7beb7b35b0c80165948b4ddc20996 100644 (file)
@@ -153,7 +153,7 @@ static virDrvOpenStatus vboxConnectOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                         unsigned int flags)
 {
-    uid_t uid = getuid();
+    uid_t uid = geteuid();
 
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
index 213059085a8b99d34880d2a26186163f987a7f19..5b1704841768e378d020805db74e3975cf5ef6c5 100644 (file)
@@ -999,7 +999,7 @@ static virDrvOpenStatus vboxConnectOpen(virConnectPtr conn,
                                         unsigned int flags)
 {
     vboxGlobalData *data = NULL;
-    uid_t uid = getuid();
+    uid_t uid = geteuid();
 
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
index bca338541c6181f0fa3a5ce87c2abc0ccad7e212..aa5774715eda326b312475cb988dced59aacae82 100644 (file)
@@ -785,7 +785,7 @@ qemuMonitorCommonTestNew(virDomainXMLOptionPtr xmlopt,
             goto error;
     }
 
-    if (virNetSocketNewListenUNIX(path, 0700, getuid(), getgid(),
+    if (virNetSocketNewListenUNIX(path, 0700, geteuid(), getegid(),
                                   &test->server) < 0)
         goto error;
 
index 5b434ba9e845c4e73a8381fc1b4602fb8a32d845..bb0e572851af5b14247ef07ddeb81b3a936d4e79 100644 (file)
@@ -220,7 +220,7 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED)
     if (virAsprintf(&path, "%s/test.sock", tmpdir) < 0)
         goto cleanup;
 
-    if (virNetSocketNewListenUNIX(path, 0700, -1, getgid(), &lsock) < 0)
+    if (virNetSocketNewListenUNIX(path, 0700, -1, getegid(), &lsock) < 0)
         goto cleanup;
 
     if (virNetSocketListen(lsock, 0) < 0)
@@ -270,7 +270,7 @@ static int testSocketUNIXAddrs(const void *data ATTRIBUTE_UNUSED)
     if (virAsprintf(&path, "%s/test.sock", tmpdir) < 0)
         goto cleanup;
 
-    if (virNetSocketNewListenUNIX(path, 0700, -1, getgid(), &lsock) < 0)
+    if (virNetSocketNewListenUNIX(path, 0700, -1, getegid(), &lsock) < 0)
         goto cleanup;
 
     if (STRNEQ(virNetSocketLocalAddrString(lsock), "127.0.0.1;0")) {