*
* Change ownership of all regular files in a directory.
*
+ * This will NOT follow any symlinks, to avoid security risks.
+ * It is assumed the process using content under @name will
+ * be unprivileged, thus less trusted than libvirt. If it is
+ * compromised it might attempt to create symlinks in @name to
+ * escalate privileges on a subsequent call to virFileChownFiles.
+ *
* Returns -1 on error, with error already reported, 0 on success.
*/
#ifndef WIN32
while ((direrr = virDirRead(dir, &ent, name)) > 0) {
g_autofree char *path = NULL;
+ struct stat sb;
path = g_build_filename(name, ent->d_name, NULL);
- if (!virFileIsRegular(path))
+ if (g_lstat(path, &sb) < 0) {
+ virReportSystemError(errno, _("cannot stat '%1$s'"), path);
+ return -1;
+ }
+
+ if (!S_ISREG(sb.st_mode))
continue;
- if (chown(path, uid, gid) < 0) {
+ if (lchown(path, uid, gid) < 0) {
virReportSystemError(errno,
_("cannot chown '%1$s' to (%2$u, %3$u)"),
ent->d_name, (unsigned int) uid,