[CRIU] [PATCH 4/5] restore: Add file owners restoring

Cyrill Gorcunov gorcunov at openvz.org
Mon Apr 9 03:49:55 EDT 2012


The base idea is trivial, once file descriptor
created the owner is read and set up.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 files.c         |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 include/files.h |    2 ++
 pipes.c         |    3 +++
 3 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/files.c b/files.c
index 53ab115..28cf1ff 100644
--- a/files.c
+++ b/files.c
@@ -103,6 +103,49 @@ void show_saved_files(void)
 		}
 }
 
+int restore_fown(pid_t pid, int fd, fown_t *fown)
+{
+	struct f_owner_ex owner;
+	uid_t uids[3];
+
+	if (fown->signum) {
+		if (fcntl(fd, F_SETSIG, fown->signum)) {
+			pr_perror("%d: Can't set signal", pid);
+			return -1;
+		}
+	}
+
+	/* May be untouched */
+	if (!fown->pid)
+		return 0;
+
+	if (getresuid(&uids[0], &uids[1], &uids[2])) {
+		pr_perror("%d: Can't get UIDs", pid);
+		return -1;
+	}
+
+	if (setresuid(fown->uid, fown->euid, uids[2])) {
+		pr_perror("%d: Can't set UIDs", pid);
+		return -1;
+	}
+
+	owner.type = fown->pid_type;
+	owner.pid = fown->pid;
+
+	if (fcntl(fd, F_SETOWN_EX, &owner)) {
+		pr_perror("%d: Can't setup %d file owner pid",
+			  pid, fd);
+		return -1;
+	}
+
+	if (setresuid(uids[0], uids[1], uids[2])) {
+		pr_perror("%d: Can't set UIDs", pid);
+		return -1;
+	}
+
+	return 0;
+}
+
 static struct reg_file_info *find_reg_file(int id)
 {
 	struct file_desc *fd;
@@ -245,6 +288,9 @@ static int open_fe_fd(struct file_desc *d)
 
 	lseek(tmp, rfi->rfe.pos, SEEK_SET);
 
+	if (restore_fown(getpid(), tmp, &rfi->rfe.fown))
+		return -1;
+
 	return tmp;
 }
 static int find_open_fe_fd(struct fdinfo_entry *fe)
diff --git a/include/files.h b/include/files.h
index 5d63db6..c1f1c32 100644
--- a/include/files.h
+++ b/include/files.h
@@ -74,4 +74,6 @@ struct file_desc;
 extern int collect_pipes(void);
 extern void mark_pipe_master(void);
 
+extern int restore_fown(pid_t pid, int fd, fown_t *fown);
+
 #endif /* FILES_H_ */
diff --git a/pipes.c b/pipes.c
index 2b18b92..868d895 100644
--- a/pipes.c
+++ b/pipes.c
@@ -315,5 +315,8 @@ out:
 	if (ret < 0)
 		return -1;
 
+	if (restore_fown(getpid(), tmp, &pi->pe.fown))
+		return -1;
+
 	return tmp;
 }
-- 
1.7.7.6



More information about the CRIU mailing list