43fe83
From ffe48a81783cc07e2f12e158e5b3985d1f313c9b Mon Sep 17 00:00:00 2001
43fe83
Message-Id: <ffe48a81783cc07e2f12e158e5b3985d1f313c9b.1383321464.git.jdenemar@redhat.com>
43fe83
From: "Daniel P. Berrange" <berrange@redhat.com>
43fe83
Date: Wed, 30 Oct 2013 17:01:41 +0000
43fe83
Subject: [PATCH] Add helpers for getting env vars in a setuid environment
43fe83
43fe83
For
43fe83
43fe83
  https://bugzilla.redhat.com/show_bug.cgi?id=1015247
43fe83
43fe83
Care must be taken accessing env variables when running
43fe83
setuid. Introduce a virGetEnvAllowSUID for env vars which
43fe83
are safe to use in a setuid environment, and another
43fe83
virGetEnvBlockSUID for vars which are not safe. Also add
43fe83
a virIsSUID helper method for any other non-env var code
43fe83
to use.
43fe83
43fe83
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
43fe83
(cherry picked from commit ae53e5d10e434e07079d7e3ba11ec654ba6a256e)
43fe83
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
43fe83
---
43fe83
 bootstrap.conf           |  1 +
43fe83
 src/libvirt_private.syms |  3 +++
43fe83
 src/util/virutil.c       | 39 +++++++++++++++++++++++++++++++++++++++
43fe83
 src/util/virutil.h       |  4 ++++
43fe83
 4 files changed, 47 insertions(+)
43fe83
43fe83
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
43fe83
index f96f126..374a332 100644
43fe83
--- a/src/libvirt_private.syms
43fe83
+++ b/src/libvirt_private.syms
43fe83
@@ -2046,6 +2046,8 @@ virFindFCHostCapableVport;
43fe83
 virFormatIntDecimal;
43fe83
 virGetDeviceID;
43fe83
 virGetDeviceUnprivSGIO;
43fe83
+virGetEnvAllowSUID;
43fe83
+virGetEnvBlockSUID;
43fe83
 virGetFCHostNameByWWN;
43fe83
 virGetGroupID;
43fe83
 virGetGroupList;
43fe83
@@ -2064,6 +2066,7 @@ virIndexToDiskName;
43fe83
 virIsCapableFCHost;
43fe83
 virIsCapableVport;
43fe83
 virIsDevMapperDevice;
43fe83
+virIsSUID;
43fe83
 virManageVport;
43fe83
 virParseNumber;
43fe83
 virParseOwnershipIds;
43fe83
diff --git a/src/util/virutil.c b/src/util/virutil.c
43fe83
index 3abcd53..a41117e 100644
43fe83
--- a/src/util/virutil.c
43fe83
+++ b/src/util/virutil.c
43fe83
@@ -2096,3 +2096,42 @@ cleanup:
43fe83
 
43fe83
     return rc;
43fe83
 }
43fe83
+
43fe83
+
43fe83
+/**
43fe83
+ * virGetEnvBlockSUID:
43fe83
+ * @name: the environment variable name
43fe83
+ *
43fe83
+ * Obtain an environment variable which is unsafe to
43fe83
+ * use when running setuid. If running setuid, a NULL
43fe83
+ * value will be returned
43fe83
+ */
43fe83
+const char *virGetEnvBlockSUID(const char *name)
43fe83
+{
43fe83
+    return secure_getenv(name);
43fe83
+}
43fe83
+
43fe83
+
43fe83
+/**
43fe83
+ * virGetEnvBlockSUID:
43fe83
+ * @name: the environment variable name
43fe83
+ *
43fe83
+ * Obtain an environment variable which is safe to
43fe83
+ * use when running setuid. The value will be returned
43fe83
+ * even when running setuid
43fe83
+ */
43fe83
+const char *virGetEnvAllowSUID(const char *name)
43fe83
+{
43fe83
+    return getenv(name);
43fe83
+}
43fe83
+
43fe83
+
43fe83
+/**
43fe83
+ * virIsSUID:
43fe83
+ * Return a true value if running setuid. Does not
43fe83
+ * check for elevated capabilities bits.
43fe83
+ */
43fe83
+bool virIsSUID(void)
43fe83
+{
43fe83
+    return getuid() != geteuid();
43fe83
+}
43fe83
diff --git a/src/util/virutil.h b/src/util/virutil.h
43fe83
index 4b06992..8739e4e 100644
43fe83
--- a/src/util/virutil.h
43fe83
+++ b/src/util/virutil.h
43fe83
@@ -172,4 +172,8 @@ int virCompareLimitUlong(unsigned long long a, unsigned long b);
43fe83
 
43fe83
 int virParseOwnershipIds(const char *label, uid_t *uidPtr, gid_t *gidPtr);
43fe83
 
43fe83
+const char *virGetEnvBlockSUID(const char *name);
43fe83
+const char *virGetEnvAllowSUID(const char *name);
43fe83
+bool virIsSUID(void);
43fe83
+
43fe83
 #endif /* __VIR_UTIL_H__ */
43fe83
-- 
43fe83
1.8.4.2
43fe83