From: "Daniel P. Berrange" <berrange(a)redhat.com>
Care must be taken accessing env variables when running
setuid. Introduce a virGetEnvAllowSUID for env vars which
are safe to use in a setuid environment, and another
virGetEnvBlockSUID for vars which are not safe. Also add
a virIsSUID helper method for any other non-env var code
to use.
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
bootstrap.conf | 1 +
src/libvirt_private.syms | 3 +++
src/util/virutil.c | 39 +++++++++++++++++++++++++++++++++++++++
src/util/virutil.h | 4 ++++
4 files changed, 47 insertions(+)
diff --git a/bootstrap.conf b/bootstrap.conf
index 8b37217..d24a714 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -93,6 +93,7 @@ recv
regex
random_r
sched
+secure_getenv
send
setenv
setsockopt
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 84c1c28..9be6f32 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1859,6 +1859,8 @@ virFindFCHostCapableVport;
virFormatIntDecimal;
virGetDeviceID;
virGetDeviceUnprivSGIO;
+virGetEnvAllowSUID;
+virGetEnvBlockSUID;
virGetFCHostNameByWWN;
virGetGroupID;
virGetGroupList;
@@ -1877,6 +1879,7 @@ virIndexToDiskName;
virIsCapableFCHost;
virIsCapableVport;
virIsDevMapperDevice;
+virIsSUID;
virManageVport;
virParseNumber;
virParseOwnershipIds;
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 854c0ad..7e7c1c2 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -2131,3 +2131,42 @@ cleanup:
return rc;
}
+
+
+/**
+ * virGetEnvBlockSUID:
+ * @name: the environment variable name
+ *
+ * Obtain an environment variable which is unsafe to
+ * use when running setuid. If running setuid, a NULL
+ * value will be returned
+ */
+const char *virGetEnvBlockSUID(const char *name)
+{
+ return secure_getenv(name);
+}
+
+
+/**
+ * virGetEnvBlockSUID:
+ * @name: the environment variable name
+ *
+ * Obtain an environment variable which is safe to
+ * use when running setuid. The value will be returned
+ * even when running setuid
+ */
+const char *virGetEnvAllowSUID(const char *name)
+{
+ return getenv(name);
+}
+
+
+/**
+ * virIsSUID:
+ * Return a true value if running setuid. Does not
+ * check for elevated capabilities bits.
+ */
+bool virIsSUID(void)
+{
+ return getuid() != geteuid();
+}
diff --git a/src/util/virutil.h b/src/util/virutil.h
index 9f6fb20..e8765b2 100644
--- a/src/util/virutil.h
+++ b/src/util/virutil.h
@@ -172,4 +172,8 @@ int virCompareLimitUlong(unsigned long long a, unsigned long long b);
int virParseOwnershipIds(const char *label, uid_t *uidPtr, gid_t *gidPtr);
+const char *virGetEnvBlockSUID(const char *name);
+const char *virGetEnvAllowSUID(const char *name);
+bool virIsSUID(void);
+
#endif /* __VIR_UTIL_H__ */
--
1.8.3.1