render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
43fe83
From f510cd0de5b55e3b2d0d65bc873d652829a7e2ed Mon Sep 17 00:00:00 2001
43fe83
Message-Id: <f510cd0de5b55e3b2d0d65bc873d652829a7e2ed.1383321465.git.jdenemar@redhat.com>
43fe83
From: "Daniel P. Berrange" <berrange@redhat.com>
43fe83
Date: Wed, 30 Oct 2013 17:01:56 +0000
43fe83
Subject: [PATCH] Remove (nearly) all use of getuid()/getgid()
43fe83
43fe83
For
43fe83
43fe83
  https://bugzilla.redhat.com/show_bug.cgi?id=1015247
43fe83
43fe83
Most of the usage of getuid()/getgid() is in cases where we are
43fe83
considering what privileges we have. As such the code should be
43fe83
using the effective IDs, not real IDs.
43fe83
43fe83
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
43fe83
(cherry picked from commit 9b0af09240618184fea5884952941217e65b824f)
43fe83
43fe83
Conflicts:
43fe83
	tests/qemumonitortestutils.c
43fe83
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
43fe83
---
43fe83
 src/libvirt.c                         |  2 +-
43fe83
 src/locking/lock_daemon.c             |  2 +-
43fe83
 src/locking/lock_driver_lockd.c       |  4 ++--
43fe83
 src/lxc/lxc_controller.c              |  2 +-
43fe83
 src/qemu/qemu_driver.c                |  6 +++---
43fe83
 src/remote/remote_driver.c            |  4 ++--
43fe83
 src/storage/storage_backend.c         |  4 ++--
43fe83
 src/storage/storage_backend_fs.c      |  4 ++--
43fe83
 src/storage/storage_backend_logical.c |  2 +-
43fe83
 src/util/virfile.c                    | 16 ++++++++--------
43fe83
 src/util/viridentity.c                |  8 ++++----
43fe83
 src/util/virstoragefile.c             |  2 +-
43fe83
 src/vbox/vbox_driver.c                |  2 +-
43fe83
 src/vbox/vbox_tmpl.c                  |  2 +-
43fe83
 tests/qemumonitortestutils.c          |  4 ++--
43fe83
 tests/virnetsockettest.c              |  4 ++--
43fe83
 16 files changed, 34 insertions(+), 34 deletions(-)
43fe83
43fe83
diff --git a/src/libvirt.c b/src/libvirt.c
43fe83
index 8a3cc42..54c90ec 100644
43fe83
--- a/src/libvirt.c
43fe83
+++ b/src/libvirt.c
43fe83
@@ -133,7 +133,7 @@ static int virConnectAuthGainPolkit(const char *privilege) {
43fe83
     int status;
43fe83
     int ret = -1;
43fe83
 
43fe83
-    if (getuid() == 0)
43fe83
+    if (geteuid() == 0)
43fe83
         return 0;
43fe83
 
43fe83
     cmd = virCommandNewArgList(POLKIT_AUTH, "--obtain", privilege, NULL);
43fe83
diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
43fe83
index 0ac16c3..d74af13 100644
43fe83
--- a/src/locking/lock_daemon.c
43fe83
+++ b/src/locking/lock_daemon.c
43fe83
@@ -1167,7 +1167,7 @@ int main(int argc, char **argv) {
43fe83
         {0, 0, 0, 0}
43fe83
     };
43fe83
 
43fe83
-    privileged = getuid() == 0;
43fe83
+    privileged = geteuid() == 0;
43fe83
 
43fe83
     if (setlocale(LC_ALL, "") == NULL ||
43fe83
         bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
43fe83
diff --git a/src/locking/lock_driver_lockd.c b/src/locking/lock_driver_lockd.c
43fe83
index 86ce2d8..f3b9467 100644
43fe83
--- a/src/locking/lock_driver_lockd.c
43fe83
+++ b/src/locking/lock_driver_lockd.c
43fe83
@@ -302,7 +302,7 @@ virLockManagerLockDaemonConnect(virLockManagerPtr lock,
43fe83
 {
43fe83
     virNetClientPtr client;
43fe83
 
43fe83
-    if (!(client = virLockManagerLockDaemonConnectionNew(getuid() == 0, program)))
43fe83
+    if (!(client = virLockManagerLockDaemonConnectionNew(geteuid() == 0, program)))
43fe83
         return NULL;
43fe83
 
43fe83
     if (virLockManagerLockDaemonConnectionRegister(lock,
43fe83
@@ -331,7 +331,7 @@ static int virLockManagerLockDaemonSetupLockspace(const char *path)
43fe83
     memset(&args, 0, sizeof(args));
43fe83
     args.path = (char*)path;
43fe83
 
43fe83
-    if (!(client = virLockManagerLockDaemonConnectionNew(getuid() == 0, &program)))
43fe83
+    if (!(client = virLockManagerLockDaemonConnectionNew(geteuid() == 0, &program)))
43fe83
         return -1;
43fe83
 
43fe83
     if (virNetClientProgramCall(program,
43fe83
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
43fe83
index 357910d..5754c92 100644
43fe83
--- a/src/lxc/lxc_controller.c
43fe83
+++ b/src/lxc/lxc_controller.c
43fe83
@@ -2353,7 +2353,7 @@ int main(int argc, char *argv[])
43fe83
         goto cleanup;
43fe83
     }
43fe83
 
43fe83
-    if (getuid() != 0) {
43fe83
+    if (geteuid() != 0) {
43fe83
         fprintf(stderr, "%s: must be run as the 'root' user\n", argv[0]);
43fe83
         goto cleanup;
43fe83
     }
43fe83
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
43fe83
index 226e32d..e7b0f37 100644
43fe83
--- a/src/qemu/qemu_driver.c
43fe83
+++ b/src/qemu/qemu_driver.c
43fe83
@@ -2796,8 +2796,8 @@ qemuOpenFileAs(uid_t fallback_uid, gid_t fallback_gid,
43fe83
     unsigned int vfoflags = 0;
43fe83
     int fd = -1;
43fe83
     int path_shared = virStorageFileIsSharedFS(path);
43fe83
-    uid_t uid = getuid();
43fe83
-    gid_t gid = getgid();
43fe83
+    uid_t uid = geteuid();
43fe83
+    gid_t gid = getegid();
43fe83
 
43fe83
     /* path might be a pre-existing block dev, in which case
43fe83
      * we need to skip the create step, and also avoid unlink
43fe83
@@ -2837,7 +2837,7 @@ qemuOpenFileAs(uid_t fallback_uid, gid_t fallback_gid,
43fe83
                qemu user is non-root, just set a flag to
43fe83
                bypass security driver shenanigans, and retry the operation
43fe83
                after doing setuid to qemu user */
43fe83
-            if ((fd != -EACCES && fd != -EPERM) || fallback_uid == getuid())
43fe83
+            if ((fd != -EACCES && fd != -EPERM) || fallback_uid == geteuid())
43fe83
                 goto error;
43fe83
 
43fe83
             /* On Linux we can also verify the FS-type of the directory. */
43fe83
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
43fe83
index 7688bf6..b3e86e1 100644
43fe83
--- a/src/remote/remote_driver.c
43fe83
+++ b/src/remote/remote_driver.c
43fe83
@@ -995,7 +995,7 @@ remoteConnectOpen(virConnectPtr conn,
43fe83
          (strstr(conn->uri->scheme, "+unix") != NULL)) &&
43fe83
         (STREQ(conn->uri->path, "/session") ||
43fe83
          STRPREFIX(conn->uri->scheme, "test+")) &&
43fe83
-        getuid() > 0) {
43fe83
+        geteuid() > 0) {
43fe83
         VIR_DEBUG("Auto-spawn user daemon instance");
43fe83
         rflags |= VIR_DRV_OPEN_REMOTE_USER;
43fe83
         if (!virIsSUID() &&
43fe83
@@ -1013,7 +1013,7 @@ remoteConnectOpen(virConnectPtr conn,
43fe83
     if (!conn->uri) {
43fe83
         VIR_DEBUG("Auto-probe remote URI");
43fe83
 #ifndef __sun
43fe83
-        if (getuid() > 0) {
43fe83
+        if (geteuid() > 0) {
43fe83
             VIR_DEBUG("Auto-spawn user daemon instance");
43fe83
             rflags |= VIR_DRV_OPEN_REMOTE_USER;
43fe83
             if (!virIsSUID() &&
43fe83
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
43fe83
index 4ebe11b..4bbf988 100644
43fe83
--- a/src/storage/storage_backend.c
43fe83
+++ b/src/storage/storage_backend.c
43fe83
@@ -527,11 +527,11 @@ virStorageBackendCreateExecCommand(virStoragePoolObjPtr pool,
43fe83
     bool filecreated = false;
43fe83
 
43fe83
     if ((pool->def->type == VIR_STORAGE_POOL_NETFS)
43fe83
-        && (((getuid() == 0)
43fe83
+        && (((geteuid() == 0)
43fe83
              && (vol->target.perms.uid != (uid_t) -1)
43fe83
              && (vol->target.perms.uid != 0))
43fe83
             || ((vol->target.perms.gid != (gid_t) -1)
43fe83
-                && (vol->target.perms.gid != getgid())))) {
43fe83
+                && (vol->target.perms.gid != getegid())))) {
43fe83
 
43fe83
         virCommandSetUID(cmd, vol->target.perms.uid);
43fe83
         virCommandSetGID(cmd, vol->target.perms.gid);
43fe83
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
43fe83
index d305b06..8bbee92 100644
43fe83
--- a/src/storage/storage_backend_fs.c
43fe83
+++ b/src/storage/storage_backend_fs.c
43fe83
@@ -786,9 +786,9 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
43fe83
 
43fe83
     /* Reflect the actual uid and gid to the config. */
43fe83
     if (pool->def->target.perms.uid == (uid_t) -1)
43fe83
-        pool->def->target.perms.uid = getuid();
43fe83
+        pool->def->target.perms.uid = geteuid();
43fe83
     if (pool->def->target.perms.gid == (gid_t) -1)
43fe83
-        pool->def->target.perms.gid = getgid();
43fe83
+        pool->def->target.perms.gid = getegid();
43fe83
 
43fe83
     if (flags != 0) {
43fe83
         ret = virStorageBackendMakeFileSystem(pool, flags);
43fe83
diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
43fe83
index 8998a11..817edd5 100644
43fe83
--- a/src/storage/storage_backend_logical.c
43fe83
+++ b/src/storage/storage_backend_logical.c
43fe83
@@ -719,7 +719,7 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
43fe83
         goto error;
43fe83
 
43fe83
     /* We can only chown/grp if root */
43fe83
-    if (getuid() == 0) {
43fe83
+    if (geteuid() == 0) {
43fe83
         if (fchown(fd, vol->target.perms.uid, vol->target.perms.gid) < 0) {
43fe83
             virReportSystemError(errno,
43fe83
                                  _("cannot set file owner '%s'"),
43fe83
diff --git a/src/util/virfile.c b/src/util/virfile.c
43fe83
index 92a81a5..33a8a6d 100644
43fe83
--- a/src/util/virfile.c
43fe83
+++ b/src/util/virfile.c
43fe83
@@ -1457,8 +1457,8 @@ virFileAccessibleAs(const char *path, int mode,
43fe83
     gid_t *groups;
43fe83
     int ngroups;
43fe83
 
43fe83
-    if (uid == getuid() &&
43fe83
-        gid == getgid())
43fe83
+    if (uid == geteuid() &&
43fe83
+        gid == getegid())
43fe83
         return access(path, mode);
43fe83
 
43fe83
     ngroups = virGetGroupList(uid, gid, &groups);
43fe83
@@ -1750,9 +1750,9 @@ virFileOpenAs(const char *path, int openflags, mode_t mode,
43fe83
 
43fe83
     /* allow using -1 to mean "current value" */
43fe83
     if (uid == (uid_t) -1)
43fe83
-        uid = getuid();
43fe83
+        uid = geteuid();
43fe83
     if (gid == (gid_t) -1)
43fe83
-        gid = getgid();
43fe83
+        gid = getegid();
43fe83
 
43fe83
     /* treat absence of both flags as presence of both for simpler
43fe83
      * calling. */
43fe83
@@ -1760,7 +1760,7 @@ virFileOpenAs(const char *path, int openflags, mode_t mode,
43fe83
         flags |= VIR_FILE_OPEN_NOFORK|VIR_FILE_OPEN_FORK;
43fe83
 
43fe83
     if ((flags & VIR_FILE_OPEN_NOFORK)
43fe83
-        || (getuid() != 0)
43fe83
+        || (geteuid() != 0)
43fe83
         || ((uid == 0) && (gid == 0))) {
43fe83
 
43fe83
         if ((fd = open(path, openflags, mode)) < 0) {
43fe83
@@ -1871,12 +1871,12 @@ virDirCreate(const char *path,
43fe83
 
43fe83
     /* allow using -1 to mean "current value" */
43fe83
     if (uid == (uid_t) -1)
43fe83
-        uid = getuid();
43fe83
+        uid = geteuid();
43fe83
     if (gid == (gid_t) -1)
43fe83
-        gid = getgid();
43fe83
+        gid = getegid();
43fe83
 
43fe83
     if ((!(flags & VIR_DIR_CREATE_AS_UID))
43fe83
-        || (getuid() != 0)
43fe83
+        || (geteuid() != 0)
43fe83
         || ((uid == 0) && (gid == 0))
43fe83
         || ((flags & VIR_DIR_CREATE_ALLOW_EXIST) && (stat(path, &st) >= 0))) {
43fe83
         return virDirCreateNoFork(path, mode, uid, gid, flags);
43fe83
diff --git a/src/util/viridentity.c b/src/util/viridentity.c
43fe83
index f681f85..4f5127c 100644
43fe83
--- a/src/util/viridentity.c
43fe83
+++ b/src/util/viridentity.c
43fe83
@@ -157,14 +157,14 @@ virIdentityPtr virIdentityGetSystem(void)
43fe83
         virAsprintf(&processtime, "%llu", timestamp) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
-    if (!(username = virGetUserName(getuid())))
43fe83
+    if (!(username = virGetUserName(geteuid())))
43fe83
         goto cleanup;
43fe83
-    if (virAsprintf(&userid, "%d", (int)getuid()) < 0)
43fe83
+    if (virAsprintf(&userid, "%d", (int)geteuid()) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
-    if (!(groupname = virGetGroupName(getgid())))
43fe83
+    if (!(groupname = virGetGroupName(getegid())))
43fe83
         goto cleanup;
43fe83
-    if (virAsprintf(&groupid, "%d", (int)getgid()) < 0)
43fe83
+    if (virAsprintf(&groupid, "%d", (int)getegid()) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
 #if WITH_SELINUX
43fe83
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
43fe83
index 0b9cec3..9453599 100644
43fe83
--- a/src/util/virstoragefile.c
43fe83
+++ b/src/util/virstoragefile.c
43fe83
@@ -572,7 +572,7 @@ virFindBackingFile(const char *start, bool start_is_dir, const char *path,
43fe83
         goto cleanup;
43fe83
     }
43fe83
 
43fe83
-    if (virFileAccessibleAs(combined, F_OK, getuid(), getgid()) < 0) {
43fe83
+    if (virFileAccessibleAs(combined, F_OK, geteuid(), getegid()) < 0) {
43fe83
         virReportSystemError(errno,
43fe83
                              _("Cannot access backing file '%s'"),
43fe83
                              combined);
43fe83
diff --git a/src/vbox/vbox_driver.c b/src/vbox/vbox_driver.c
43fe83
index 9d07574..4978913 100644
43fe83
--- a/src/vbox/vbox_driver.c
43fe83
+++ b/src/vbox/vbox_driver.c
43fe83
@@ -153,7 +153,7 @@ static virDrvOpenStatus vboxConnectOpen(virConnectPtr conn,
43fe83
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
43fe83
                                         unsigned int flags)
43fe83
 {
43fe83
-    uid_t uid = getuid();
43fe83
+    uid_t uid = geteuid();
43fe83
 
43fe83
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
43fe83
 
43fe83
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
43fe83
index 2130590..5b17048 100644
43fe83
--- a/src/vbox/vbox_tmpl.c
43fe83
+++ b/src/vbox/vbox_tmpl.c
43fe83
@@ -999,7 +999,7 @@ static virDrvOpenStatus vboxConnectOpen(virConnectPtr conn,
43fe83
                                         unsigned int flags)
43fe83
 {
43fe83
     vboxGlobalData *data = NULL;
43fe83
-    uid_t uid = getuid();
43fe83
+    uid_t uid = geteuid();
43fe83
 
43fe83
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
43fe83
 
43fe83
diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
43fe83
index 56368a2..1bcdb1f 100644
43fe83
--- a/tests/qemumonitortestutils.c
43fe83
+++ b/tests/qemumonitortestutils.c
43fe83
@@ -482,8 +482,8 @@ qemuMonitorTestPtr qemuMonitorTestNew(bool json, virDomainXMLOptionPtr xmlopt)
43fe83
 
43fe83
     if (virNetSocketNewListenUNIX(path,
43fe83
                                   0700,
43fe83
-                                  getuid(),
43fe83
-                                  getgid(),
43fe83
+                                  geteuid(),
43fe83
+                                  getegid(),
43fe83
                                   &test->server) < 0)
43fe83
         goto error;
43fe83
 
43fe83
diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c
43fe83
index 5b434ba..bb0e572 100644
43fe83
--- a/tests/virnetsockettest.c
43fe83
+++ b/tests/virnetsockettest.c
43fe83
@@ -220,7 +220,7 @@ static int testSocketUNIXAccept(const void *data ATTRIBUTE_UNUSED)
43fe83
     if (virAsprintf(&path, "%s/test.sock", tmpdir) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
-    if (virNetSocketNewListenUNIX(path, 0700, -1, getgid(), &lsock) < 0)
43fe83
+    if (virNetSocketNewListenUNIX(path, 0700, -1, getegid(), &lsock) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
     if (virNetSocketListen(lsock, 0) < 0)
43fe83
@@ -270,7 +270,7 @@ static int testSocketUNIXAddrs(const void *data ATTRIBUTE_UNUSED)
43fe83
     if (virAsprintf(&path, "%s/test.sock", tmpdir) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
-    if (virNetSocketNewListenUNIX(path, 0700, -1, getgid(), &lsock) < 0)
43fe83
+    if (virNetSocketNewListenUNIX(path, 0700, -1, getegid(), &lsock) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
     if (STRNEQ(virNetSocketLocalAddrString(lsock), "127.0.0.1;0")) {
43fe83
-- 
43fe83
1.8.4.2
43fe83