[CRIU] [PATCH 09/11] files, pipes: Add dump and restore of file owners

Cyrill Gorcunov gorcunov at openvz.org
Sun Mar 25 09:35:22 EDT 2012


While fcntl provides almost all data file
owner handler might have, a missing piece reminds
uid and euid of owner.

For this sake a kernel patch is needed, which means
the crtools will refuse to work on old kernels (before
crtools-v3.3).

This patch brings dump/show/restore procedure of
file owners. Noticable detail of the patch -- the
image format is extended, again ;)

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 cr-dump.c       |   39 ++++++++++++++++++++++++++++++++++++++-
 cr-restore.c    |    6 ++++++
 cr-show.c       |    9 +++++++++
 files.c         |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 include/files.h |    1 +
 include/image.h |   10 ++++++++++
 6 files changed, 110 insertions(+), 1 deletions(-)

diff --git a/cr-dump.c b/cr-dump.c
index 9a33ee7..fe57383 100644
--- a/cr-dump.c
+++ b/cr-dump.c
@@ -124,6 +124,7 @@ struct fd_parms {
 	unsigned long	pos;
 	unsigned int	flags;
 	unsigned int	type;
+	fown_t		fown;
 
 	u64		id;
 	pid_t		pid;
@@ -158,6 +159,7 @@ static int dump_one_reg_file(const struct fd_parms *p, int lfd,
 	e.pos	= p->pos;
 	e.addr	= p->fd_name;
 	e.id	= FD_ID_INVALID;
+	e.fown	= p->fown;
 
 	if (likely(!fd_is_special(&e))) {
 		u64 id;
@@ -294,6 +296,7 @@ static int dump_one_pipe(const struct fd_parms *p, unsigned int id, int lfd,
 	e.fd		= p->fd_name;
 	e.pipeid	= id;
 	e.flags		= p->flags;
+	e.fown		= p->fown;
 
 	if (p->flags & O_WRONLY) {
 		e.bytes = 0;
@@ -311,8 +314,13 @@ err:
 	return ret;
 }
 
-static void fill_fd_params(pid_t pid, int fd, int lfd, struct fd_parms *p)
+static int fill_fd_params(pid_t pid, int fd, int lfd, struct fd_parms *p)
 {
+	struct f_owner_ex owner_ex;
+	u32 v[2];
+
+	memzero(p, sizeof(*p));
+
 	p->fd_name	= fd;
 	p->pos		= lseek(lfd, 0, SEEK_CUR);
 	p->flags	= fcntl(lfd, F_GETFL);
@@ -321,6 +329,35 @@ static void fill_fd_params(pid_t pid, int fd, int lfd, struct fd_parms *p)
 
 	pr_info("%d fdinfo %d: pos: %16lx flags: %16o\n",
 		pid, fd, p->pos, p->flags);
+
+	p->fown.signum = fcntl(lfd, F_GETSIG, 0);
+	if (p->fown.signum < 0) {
+		pr_perror("Can't get owner signum on %d/%d\n", pid, fd);
+		return -1;
+	}
+
+	if (fcntl(lfd, F_GETOWN_EX, (long)&owner_ex)) {
+		pr_perror("Can't get owners on %d/%d\n", pid, fd);
+		return -1;
+	}
+
+	/*
+	 * Simple case -- nothing is changed.
+	 */
+	if (owner_ex.pid == 0)
+		return 0;
+
+	if (fcntl(lfd, F_GETOWNER_UIDS, (long)&v)) {
+		pr_perror("Can't get owner uids on %d/%d\n", pid, fd);
+		return -1;
+	}
+
+	p->fown.uid		= v[0];
+	p->fown.euid		= v[1];
+	p->fown.pid_type	= owner_ex.type;
+	p->fown.pid		= owner_ex.pid;
+
+	return 0;
 }
 
 static int dump_one_fd(pid_t pid, int fd, int lfd,
diff --git a/cr-restore.c b/cr-restore.c
index 3c29fcc..b34ba5c 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -704,6 +704,9 @@ static int create_pipe(int pid, struct pipe_entry *e, struct pipe_info *pi, int
 	if (tmp < 0)
 		return -1;
 
+	if (restore_fown(pid, e->fd, &e->fown))
+		return -1;
+
 	pr_info("\t%d: All is ok - reopening pipe for %d\n", pid, e->fd);
 
 	return 0;
@@ -760,6 +763,9 @@ out:
 	if (tmp < 0)
 		return -1;
 
+	if (restore_fown(pid, e->fd, &e->fown))
+		return -1;
+
 	return 0;
 
 }
diff --git a/cr-show.c b/cr-show.c
index a19b2b2..f9c8211 100644
--- a/cr-show.c
+++ b/cr-show.c
@@ -98,6 +98,11 @@ static void show_files(int fd_files)
 		}
 
 		pr_msg("\n");
+
+		pr_msg("\touid: %4x oeuid: %4x osignum: %4x "
+		       "opid_type: %2x opid: %8x\n",
+		       e.fown.uid, e.fown.euid, e.fown.signum,
+		       e.fown.pid_type, e.fown.pid);
 	}
 
 out:
@@ -119,6 +124,10 @@ static void show_pipes(int fd_pipes)
 			goto out;
 		pr_msg("fd: %8x pipeid: %8x flags: %8x bytes: %8x\n",
 		       e.fd, e.pipeid, e.flags, e.bytes);
+		pr_msg("\touid: %4x oeuid: %4x osignum: %4x "
+		       "opid_type: %2x opid: %8x\n",
+		       e.fown.uid, e.fown.euid, e.fown.signum,
+		       e.fown.pid_type, e.fown.pid);
 		if (e.bytes)
 			lseek(fd_pipes, e.bytes, SEEK_CUR);
 	}
diff --git a/files.c b/files.c
index 5b25a77..762d8f4 100644
--- a/files.c
+++ b/files.c
@@ -127,6 +127,49 @@ static int collect_fd(int pid, struct fdinfo_entry *e)
 	return 0;
 }
 
+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;
+}
+
 int prepare_fd_pid(int pid)
 {
 	int fdinfo_fd, ret = 0;
@@ -178,6 +221,9 @@ static int open_fe_fd(struct fdinfo_entry *fe, int fd)
 
 	lseek(tmp, fe->pos, SEEK_SET);
 
+	if (restore_fown(getpid(), tmp, &fe->fown))
+		return -1;
+
 	return tmp;
 }
 
diff --git a/include/files.h b/include/files.h
index e040380..563f22a 100644
--- a/include/files.h
+++ b/include/files.h
@@ -41,6 +41,7 @@ extern int prepare_fds(int pid);
 extern int prepare_fd_pid(int pid);
 extern int prepare_shared_fdinfo(void);
 extern int get_filemap_fd(int pid, struct vma_entry *vma_entry);
+extern int restore_fown(pid_t pid, int fd, fown_t *fown);
 
 extern int self_exe_fd;
 
diff --git a/include/image.h b/include/image.h
index f2daae9..a3ada47 100644
--- a/include/image.h
+++ b/include/image.h
@@ -44,6 +44,14 @@ enum fd_types {
 #define PAGE_RSS	1
 #define PAGE_ANON	2
 
+typedef struct {
+	u32	uid;
+	u32	euid;
+	u32	signum;
+	u32	pid_type;
+	u32	pid;
+} __packed fown_t;
+
 struct fdinfo_entry {
 	u8	type;
 	u8	len;
@@ -51,6 +59,7 @@ struct fdinfo_entry {
 	u32	pos;
 	u64	addr;
 	u64	id;
+	fown_t	fown;
 	u8	name[0];
 } __packed;
 
@@ -70,6 +79,7 @@ struct pipe_entry {
 	u32	pipeid;
 	u32	flags;
 	u32	bytes;
+	fown_t	fown;
 	u8	data[0];
 } __packed;
 
-- 
1.7.7.6



More information about the CRIU mailing list