Add helpers for getting env vars in a setuid environment

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@redhat.com>
This commit is contained in:
Daniel P. Berrange 2013-10-09 10:52:39 +01:00
parent 57687fd6bf
commit ae53e5d10e
4 changed files with 47 additions and 0 deletions

View File

@ -93,6 +93,7 @@ recv
regex regex
random_r random_r
sched sched
secure_getenv
send send
setenv setenv
setsockopt setsockopt

View File

@ -1859,6 +1859,8 @@ virFindFCHostCapableVport;
virFormatIntDecimal; virFormatIntDecimal;
virGetDeviceID; virGetDeviceID;
virGetDeviceUnprivSGIO; virGetDeviceUnprivSGIO;
virGetEnvAllowSUID;
virGetEnvBlockSUID;
virGetFCHostNameByWWN; virGetFCHostNameByWWN;
virGetGroupID; virGetGroupID;
virGetGroupList; virGetGroupList;
@ -1877,6 +1879,7 @@ virIndexToDiskName;
virIsCapableFCHost; virIsCapableFCHost;
virIsCapableVport; virIsCapableVport;
virIsDevMapperDevice; virIsDevMapperDevice;
virIsSUID;
virManageVport; virManageVport;
virParseNumber; virParseNumber;
virParseOwnershipIds; virParseOwnershipIds;

View File

@ -2131,3 +2131,42 @@ cleanup:
return rc; 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();
}

View File

@ -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); 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__ */ #endif /* __VIR_UTIL_H__ */