From ffe48a81783cc07e2f12e158e5b3985d1f313c9b Mon Sep 17 00:00:00 2001 Message-Id: From: "Daniel P. Berrange" Date: Wed, 30 Oct 2013 17:01:41 +0000 Subject: [PATCH] Add helpers for getting env vars in a setuid environment For https://bugzilla.redhat.com/show_bug.cgi?id=1015247 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 (cherry picked from commit ae53e5d10e434e07079d7e3ba11ec654ba6a256e) Signed-off-by: Jiri Denemark --- 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/src/libvirt_private.syms b/src/libvirt_private.syms index f96f126..374a332 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2046,6 +2046,8 @@ virFindFCHostCapableVport; virFormatIntDecimal; virGetDeviceID; virGetDeviceUnprivSGIO; +virGetEnvAllowSUID; +virGetEnvBlockSUID; virGetFCHostNameByWWN; virGetGroupID; virGetGroupList; @@ -2064,6 +2066,7 @@ virIndexToDiskName; virIsCapableFCHost; virIsCapableVport; virIsDevMapperDevice; +virIsSUID; virManageVport; virParseNumber; virParseOwnershipIds; diff --git a/src/util/virutil.c b/src/util/virutil.c index 3abcd53..a41117e 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -2096,3 +2096,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 4b06992..8739e4e 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -172,4 +172,8 @@ int virCompareLimitUlong(unsigned long long a, unsigned 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.4.2