pid_t pid = 0;
int status, ret = 0;
int forkRet = 0;
+ gid_t *groups;
+ int ngroups;
if (uid == getuid() &&
gid == getgid())
return access(path, mode);
+ ngroups = virGetGroupList(uid, gid, &groups);
+ if (ngroups < 0)
+ return -1;
+
forkRet = virFork(&pid);
if (pid < 0) {
+ VIR_FREE(groups);
return -1;
}
if (pid) { /* parent */
+ VIR_FREE(groups);
if (virProcessWait(pid, &status) < 0) {
/* virProcessWait() already
* reported error */
goto childerror;
}
- if (virSetUIDGID(uid, gid) < 0) {
+ if (virSetUIDGID(uid, gid, groups, ngroups) < 0) {
ret = errno;
goto childerror;
}
int fd = -1;
int pair[2] = { -1, -1 };
int forkRet;
+ gid_t *groups;
+ int ngroups;
/* parent is running as root, but caller requested that the
* file be opened as some other user and/or group). The
* following dance avoids problems caused by root-squashing
* NFS servers. */
+ ngroups = virGetGroupList(uid, gid, &groups);
+ if (ngroups < 0)
+ return -errno;
+
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) {
ret = -errno;
virReportSystemError(errno,
_("failed to create socket needed for '%s'"),
path);
+ VIR_FREE(groups);
return ret;
}
/* set desired uid/gid, then attempt to create the file */
- if (virSetUIDGID(uid, gid) < 0) {
+ if (virSetUIDGID(uid, gid, groups, ngroups) < 0) {
ret = -errno;
goto childerror;
}
/* parent */
+ VIR_FREE(groups);
VIR_FORCE_CLOSE(pair[1]);
do {
pid_t pid;
int waitret;
int status, ret = 0;
+ gid_t *groups;
+ int ngroups;
/* allow using -1 to mean "current value" */
if (uid == (uid_t) -1)
return virDirCreateNoFork(path, mode, uid, gid, flags);
}
+ ngroups = virGetGroupList(uid, gid, &groups);
+ if (ngroups < 0)
+ return -errno;
+
int forkRet = virFork(&pid);
if (pid < 0) {
ret = -errno;
+ VIR_FREE(groups);
return ret;
}
if (pid) { /* parent */
/* wait for child to complete, and retrieve its exit code */
+ VIR_FREE(groups);
while ((waitret = waitpid(pid, &status, 0) == -1) && (errno == EINTR));
if (waitret == -1) {
ret = -errno;
/* set desired uid/gid, then attempt to create the directory */
- if (virSetUIDGID(uid, gid) < 0) {
+ if (virSetUIDGID(uid, gid, groups, ngroups) < 0) {
ret = -errno;
goto childerror;
}
goto cleanup;
}
-/* Set the real and effective uid and gid to the given values, and call
- * initgroups so that the process has all the assumed group membership of
- * that uid. return 0 on success, -1 on failure (the original system error
- * remains in errno).
+/* Set the real and effective uid and gid to the given values, as well
+ * as all the supplementary groups, so that the process has all the
+ * assumed group membership of that uid. Return 0 on success, -1 on
+ * failure (the original system error remains in errno).
*/
int
-virSetUIDGID(uid_t uid, gid_t gid)
+virSetUIDGID(uid_t uid, gid_t gid, gid_t *groups ATTRIBUTE_UNUSED,
+ int ngroups ATTRIBUTE_UNUSED)
{
- int err;
- char *buf = NULL;
-
- if (gid > 0) {
- if (setregid(gid, gid) < 0) {
- virReportSystemError(err = errno,
- _("cannot change to '%d' group"),
- (unsigned int) gid);
- goto error;
- }
+ if (gid != (gid_t)-1 && setregid(gid, gid) < 0) {
+ virReportSystemError(errno,
+ _("cannot change to '%u' group"),
+ (unsigned int) gid);
+ return -1;
}
- if (uid > 0) {
-# ifdef HAVE_INITGROUPS
- struct passwd pwd, *pwd_result;
- size_t bufsize;
- int rc;
-
- bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
- if (bufsize == -1)
- bufsize = 16384;
-
- if (VIR_ALLOC_N(buf, bufsize) < 0) {
- virReportOOMError();
- err = ENOMEM;
- goto error;
- }
- while ((rc = getpwuid_r(uid, &pwd, buf, bufsize,
- &pwd_result)) == ERANGE) {
- if (VIR_RESIZE_N(buf, bufsize, bufsize, bufsize) < 0) {
- virReportOOMError();
- err = ENOMEM;
- goto error;
- }
- }
-
- if (rc) {
- virReportSystemError(err = rc, _("cannot getpwuid_r(%d)"),
- (unsigned int) uid);
- goto error;
- }
-
- if (!pwd_result) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("getpwuid_r failed to retrieve data "
- "for uid '%d'"),
- (unsigned int) uid);
- err = EINVAL;
- goto error;
- }
-
- if (initgroups(pwd.pw_name, pwd.pw_gid) < 0) {
- virReportSystemError(err = errno,
- _("cannot initgroups(\"%s\", %d)"),
- pwd.pw_name, (unsigned int) pwd.pw_gid);
- goto error;
- }
+# if HAVE_SETGROUPS
+ if (ngroups && setgroups(ngroups, groups) < 0) {
+ virReportSystemError(errno, "%s",
+ _("cannot set supplemental groups"));
+ return -1;
+ }
# endif
- if (setreuid(uid, uid) < 0) {
- virReportSystemError(err = errno,
- _("cannot change to uid to '%d'"),
- (unsigned int) uid);
- goto error;
- }
+
+ if (uid != (uid_t)-1 && setreuid(uid, uid) < 0) {
+ virReportSystemError(errno,
+ _("cannot change to uid to '%u'"),
+ (unsigned int) uid);
+ return -1;
}
- VIR_FREE(buf);
return 0;
-
-error:
- VIR_FREE(buf);
- errno = err;
- return -1;
}
#else /* ! HAVE_GETPWUID_R */
int
virSetUIDGID(uid_t uid ATTRIBUTE_UNUSED,
- gid_t gid ATTRIBUTE_UNUSED)
+ gid_t gid ATTRIBUTE_UNUSED,
+ gid_t *groups ATTRIBUTE_UNUSED,
+ int ngroups ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("virSetUIDGID is not available"));