Skip to contentSkip to conversation

a start to credentials c/r

PATCH In review Serge E. Hallyn 5 participants 17 messages May 26, 2009 – May 28, 2009 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover a start to credentials c/r

Following is the next version of the credentials c/r patchset, on top of the c/r patchset at
git://git.ncl.cs.columbia.edu/pub/git/linux-cr.git

It implements checkpoint and restart of user, user namespaces, groups, supplementary groups, and struct cred.

There is a question as to what to do about LSM data at restart. Right now I'm ignoring it, which means that prepare_creds() should ensure that the restart tasks get the context of the task calling sys_restart(). I suspect the right thing to do is to add two new LSM hooks, one which checks current's authorization to restart from the checkpoint file, and one which determines the task->cred->security filed based upon any of:
1. current_security() of the task calling sys_restart()
2. the task->cred->security checkpointed in the ckpt file
3. the ->security of the checkpoint file

Oren, I think this version has all the changes you asked for except for restoring cred info for sysvipc.

thanks,
-serge

#1 lore raw

PATCH 1/8 cr: break out new_user_ns()

Break out the core function which checks privilege and (if allowed) creates a new user namespace, with the passed-in creating user_struct. Note that a user_namespace, unlike other namespace pointers, is not stored in the nsproxy. Rather it is purely a property of user_structs.

This will let us keep the task restore code simpler.

---
 include/linux/user_namespace.h |    8 ++++++
 kernel/user_namespace.c        |   53 ++++++++++++++++++++++++++++------------
 2 files changed, 45 insertions(+), 16 deletions(-)
include/linux/user_namespace.h +8 -0
index cc4f453..a2b82d5 100644
@@ -20,6 +20,8 @@ extern struct user_namespace init_user_ns;
 
 #ifdef CONFIG_USER_NS
 
+struct user_namespace *new_user_ns(struct user_struct *creator,
+				   struct user_struct **newroot);
 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
 {
 	if (ns)
@@ -38,6 +40,12 @@ static inline void put_user_ns(struct user_namespace *ns)
 
 #else
 
+static inline struct user_namespace *new_user_ns(struct user_struct *creator,
+				   struct user_struct **newroot)
+{
+	return -EINVAL;
+}
+
 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
 {
 	return &init_user_ns;
kernel/user_namespace.c +37 -16

80 lines not on this page · raw message

Signature · 1 line
1.6.1
#2 lore raw

PATCH 2/8 cr: split core function out of some set*{u,g}id functions

In reply to Serge E. Hallyn

When restarting tasks, we want to be able to change xuid and xgid in a struct cred, and do so with security checks. Break the core functionality of set{fs,res}{u,g}id into cred_setX which performs the access checks based on current_cred(), but performs the requested change on a passed-in cred.

This will allow us to securely construct struct creds based on a checkpoint image, constrained by the caller's permissions, and apply them to the caller at the end of sys_restart().

---
 include/linux/cred.h |    8 +++
 kernel/cred.c        |  114 ++++++++++++++++++++++++++++++++++++++++++
 kernel/sys.c         |  134 ++++++++------------------------------------------
 3 files changed, 143 insertions(+), 113 deletions(-)
include/linux/cred.h +8 -0
index 3282ee4..bc5ffc2 100644
@@ -20,6 +20,9 @@ struct user_struct;
 struct cred;
 struct inode;
 
+/* defined in sys.c, used in cred_setresuid */
+extern int set_user(struct cred *new);
+
 /*
  * COW Supplementary groups list
  */
@@ -343,4 +346,9 @@ do {						\
 	*(_fsgid) = __cred->fsgid;		\
 } while(0)
 
+int cred_setresuid(struct cred *new, uid_t ruid, uid_t euid, uid_t suid);
+int cred_setresgid(struct cred *new, gid_t rgid, gid_t egid, gid_t sgid);
+int cred_setfsuid(struct cred *new, uid_t uid, uid_t *old_fsuid);
+int cred_setfsgid(struct cred *new, gid_t gid, gid_t *old_fsgid);
+
 #endif /* _LINUX_CRED_H */
kernel/cred.c +114 -0

117 lines not on this page · raw message

kernel/sys.c +21 -113

210 lines not on this page · raw message

Signature · 1 line
1.6.1
#3 lore raw

PATCH 3/8 cr: capabilities: define checkpoint and restore fns

In reply to Serge E. Hallyn

An application checkpoint image will store capability sets (and the bounding set) as __u64s. Define checkpoint and restart functions to translate between those and kernel_cap_t's.

Define a common function do_capset_tocred() which applies capability set changes to a passed-in struct cred.

The restore function uses do_capset_tocred() to apply the restored capabilities to the struct cred being crafted, subject to the current task's (task executing sys_restart()) permissions.

TODO: one day we'll want to c/r the securebits as well.

---
 include/linux/capability.h |    5 ++
 kernel/capability.c        |   94 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 86 insertions(+), 13 deletions(-)
include/linux/capability.h +5 -0
index c302110..572b5a0 100644
@@ -536,6 +536,11 @@ extern const kernel_cap_t __cap_empty_set;
 extern const kernel_cap_t __cap_full_set;
 extern const kernel_cap_t __cap_init_eff_set;
 
+extern void checkpoint_save_cap(__u64 *dest, kernel_cap_t src);
+struct cred;
+extern int checkpoint_restore_cap(__u64 e, __u64 i, __u64 p, __u64 x,
+				struct cred *cred);
+
 /**
  * has_capability - Determine if a task has a superior capability available
  * @t: The task in question
kernel/capability.c +81 -13

116 lines not on this page · raw message

Signature · 1 line
1.6.1
#4 lore raw

PATCH 4/8 groups: move code to kernel/groups.c

In reply to Serge E. Hallyn

Move supplementary groups implementation to kernel/groups.c . kernel/sys.c already accumulated quite a few random stuff.

Do strictly copy/paste + add required headers to compile. Compile-tested on many configs and archs.

---
 kernel/Makefile |    1 +
 kernel/groups.c |  288 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sys.c    |  283 ------------------------------------------------------
 3 files changed, 289 insertions(+), 283 deletions(-)
 create mode 100644 kernel/groups.c
kernel/Makefile +1 -0
index 6bc638d..4d4f741 100644
@@ -11,6 +11,7 @@ obj-y     = sched.o fork.o exec_domain.o panic.o printk.o \
 	    hrtimer.o rwsem.o nsproxy.o srcu.o semaphore.o \
 	    notifier.o ksysfs.o pm_qos_params.o sched_clock.o cred.o \
 	    async.o
+obj-y += groups.o
 
 ifdef CONFIG_FUNCTION_TRACER
 # Do not trace debug files and internal ftrace files
kernel/groups.c new file +288 -0

288 lines not on this page · raw message

kernel/sys.c +0 -283

289 lines not on this page · raw message

Signature · 1 line
1.6.1
#5 lore raw

PATCH 5/8 groups: allow compilation on s390x

In reply to Serge E. Hallyn
---
 kernel/groups.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/groups.c +1 -0
index 1b95b2f..14ebc6a 100644
@@ -1,6 +1,7 @@
 /*
  * Supplementary group IDs
  */
+#include <linux/init.h>
 #include <linux/cred.h>
 #include <linux/module.h>
 #include <linux/slab.h>
Signature · 1 line
1.6.1
#6 lore raw

Re: [PATCH 5/8] groups: allow compilation on s390x

Quoted 19 lines · Serge E. Hallyn
---
 kernel/groups.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/groups.c +1 -0
index 1b95b2f..14ebc6a 100644
@@ -1,6 +1,7 @@
 /*
  * Supplementary group IDs
  */
+#include <linux/init.h>
 #include <linux/cred.h>
 #include <linux/module.h>
 #include <linux/slab.h>
Signature · 1 line
1.6.1

As noted by Alexey, this is wrong, and the prob was actually fixed by the following patch he'd also sent last friday, so please replace this patch with the following:

From: Alexey Dobriyan <[email protected]>
Subject: [PATCH 01/38] cred: #include init.h in cred.h

cred.h can't be included as first header because it uses __init and doesn't include init.h which is enough to break compilation on at least ia64.

---
 include/linux/cred.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
include/linux/cred.h +1 -0
index 3282ee4..4fa9996 100644
@@ -13,6 +13,7 @@
 #define _LINUX_CRED_H
 
 #include <linux/capability.h>
+#include <linux/init.h>
 #include <linux/key.h>
 #include <asm/atomic.h>
 
Signature · 6 lines
1.5.6.5

_______________________________________________
Containers mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): 20090526173242.GA13757-r/[email protected]

#8 lore raw

PATCH 6/8 cr: checkpoint and restore task credentials

This patch adds the checkpointing and restart of credentials (uids, gids, and capabilities) to Oren's c/r patchset (on top of v14). It goes to great pains to re-use (and define when needed) common helpers, in order to make sure that as security code is modified, the cr code will be updated. Some of the helpers should still be moved (i.e. _creds() functions should be in kernel/cred.c).

When building the credentials for the restarted process, I
1. create a new struct cred as a copy of the running task's cred (using prepare_cred())
2. always authorize any changes to the new struct cred based on the permissions of current_cred() (not the current transient state of the new cred).

While this may mean that certain transient_cred1->transient_cred2 states are allowed which otherwise wouldn't be allowed, the fact remains that current_cred() is allowed to transition to transient_cred2.

The reconstructed creds are applied to the task at the very end of the sys_restart call. This ensures that any objects which need to be re-created (file, socket, etc) are re-created using the creds of the task calling sys_restart - preventing an unpriv user from creating a privileged object, and ensuring that a root task can restart a process which had started out privileged, created some privileged objects, then dropped its privilege.

With these patches, the root user can restart checkpoint images (created by either hallyn or root) of user hallyn's tasks, resulting in a program owned by hallyn.

Plenty of bugs to be found, no doubt.

TODO:
* fully remove limit on # of groups in groupinfo at restore_read_groupinfo().
* restore uid info on sysvipc objects

Changelog:
May 26: Move group, user, userns, creds c/r functions out
of checkpoint/process.c and into the appropriate files.
May 26: Define struct ckpt_hdr_task_creds and move task cred
objref c/r into {checkpoint_restore}_task_shared().
May 26: Take cred refs around checkpoint_write_creds() May 20: Remove the limit on number of groups in groupinfo
at checkpoint time
May 20: Remove the depth limit on empty user namespaces May 20: Better document checkpoint_user
May 18: fix more refcounting: if (userns 5, uid 0) had
no active tasks or child user_namespaces, then it shouldn't exist at restart or it, its namespace, and its whole chain of creators will be leaked.
May 14: fix some refcounting:
1. a new user_ns needs a ref to remain pinned by its root user
2. current_user_ns needs an extra ref bc objhash drops two on restart
3. cred needs a ref for the real credentials bc commit_creds eats one ref.
May 13: folded in fix to userns refcounting.

---
 checkpoint/objhash.c             |  118 +++++++++++++++++++++++++++++
 checkpoint/process.c             |  154 +++++++++++++++++++++++++++++++++++++-
 include/linux/checkpoint.h       |   11 +++
 include/linux/checkpoint_hdr.h   |   67 ++++++++++++++++
 include/linux/checkpoint_types.h |    2 +
 include/linux/cred.h             |   11 +++
 include/linux/sched.h            |    6 ++
 include/linux/user_namespace.h   |    6 ++
 kernel/cred.c                    |  120 +++++++++++++++++++++++++++++
 kernel/groups.c                  |   56 ++++++++++++++
 kernel/user.c                    |  147 ++++++++++++++++++++++++++++++++++++
 kernel/user_namespace.c          |   86 +++++++++++++++++++++
 12 files changed, 782 insertions(+), 2 deletions(-)
checkpoint/objhash.c +118 -0
index 5618fff..44b948a 100644
@@ -16,6 +16,7 @@
 #include <linux/file.h>
 #include <linux/sched.h>
 #include <linux/ipc_namespace.h>
+#include <linux/user_namespace.h>
 #include <linux/checkpoint.h>
 #include <linux/checkpoint_hdr.h>
 
@@ -155,6 +156,71 @@ static int obj_ipc_ns_users(void *ptr)
 	return atomic_read(&((struct ipc_namespace *) ptr)->count);
 }
 
+static int obj_cred_grab(void *ptr)
+{
+	get_cred((struct cred *) ptr);
+	return 0;
+}
+
+static void obj_cred_drop(void *ptr)
+{
+	put_cred((struct cred *) ptr);
+}
+
+static int obj_cred_users(void *ptr)
+{
+	return atomic_read(&((struct cred *) ptr)->usage);
+}
+
+static int obj_user_grab(void *ptr)
+{
+	struct user_struct *u = ptr;
+	(void) get_uid(u);
+	return 0;
+}
+
+static void obj_user_drop(void *ptr)
+{
+	free_uid((struct user_struct *) ptr);
+}
+
+static int obj_user_users(void *ptr)
+{
+	return atomic_read(&((struct user_struct *) ptr)->__count);
+}
+
+static int obj_userns_grab(void *ptr)
+{
+	get_user_ns((struct user_namespace *) ptr);
+	return 0;
+}
+
+static void obj_userns_drop(void *ptr)
+{
+	put_user_ns((struct user_namespace *) ptr);
+}
+
+static int obj_user_ns_users(void *ptr)
+{
+	return atomic_read(&((struct user_namespace *) ptr)->kref.refcount);
+}
+
+static int obj_groupinfo_grab(void *ptr)
+{
+	get_group_info((struct group_info *) ptr);
+	return 0;
+}
+
+static void obj_groupinfo_drop(void *ptr)
+{
+	put_group_info((struct group_info *) ptr);
+}
+
+static int obj_groupinfo_users(void *ptr)
+{
+	return atomic_read(&((struct group_info *) ptr)->usage);
+}
+
 static struct ckpt_obj_ops ckpt_obj_ops[] = {
 	/* ignored object */
 	{
@@ -221,6 +287,46 @@ static struct ckpt_obj_ops ckpt_obj_ops[] = {
 		.checkpoint = checkpoint_bad,
 		.restore = restore_bad,
 	},
+	/* user_ns object */
+	{
+		.obj_name = "USER_NS",
+		.obj_type = CKPT_OBJ_USER_NS,
+		.ref_drop = obj_userns_drop,
+		.ref_grab = obj_userns_grab,
+		.ref_users = obj_user_ns_users,
+		.checkpoint = checkpoint_userns,
+		.restore = restore_userns,
+	},
+	/* struct cred */
+	{
+		.obj_name = "CRED",
+		.obj_type = CKPT_OBJ_CRED,
+		.ref_drop = obj_cred_drop,
+		.ref_grab = obj_cred_grab,
+		.ref_users = obj_cred_users,
+		.checkpoint = checkpoint_cred,
+		.restore = restore_cred,
+	},
+	/* user object */
+	{
+		.obj_name = "USER",
+		.obj_type = CKPT_OBJ_USER,
+		.ref_drop = obj_user_drop,
+		.ref_grab = obj_user_grab,
+		.ref_users = obj_user_users,
+		.checkpoint = checkpoint_user,
+		.restore = restore_user,
+	},
+	/* struct groupinfo */
+	{
+		.obj_name = "GROUPINFO",
+		.obj_type = CKPT_OBJ_GROUPINFO,
+		.ref_drop = obj_groupinfo_drop,
+		.ref_grab = obj_groupinfo_grab,
+		.ref_users = obj_groupinfo_users,
+		.checkpoint = checkpoint_groupinfo,
+		.restore = restore_groupinfo,
+	},
 };
 
 
@@ -290,6 +396,18 @@ static struct ckpt_obj *obj_find_by_ptr(struct ckpt_ctx *ctx, void *ptr)
 	return NULL;
 }
 
+/*
+ * look up an obj and return objref if in hash, else
+ * return 0.  Used during checkpoint.
+ */
+int obj_lookup(struct ckpt_ctx *ctx, void *ptr)
+{
+	struct ckpt_obj *obj = obj_find_by_ptr(ctx, ptr);
+	if (obj)
+		return obj->objref;
+	return 0;
+}
+
 static struct ckpt_obj *obj_find_by_objref(struct ckpt_ctx *ctx, int objref)
 {
 	struct hlist_head *h;
checkpoint/process.c +152 -2

220 lines not on this page · raw message

include/linux/checkpoint.h +11 -0
index 9660c54..b26b738 100644
@@ -58,6 +58,7 @@ extern void *ckpt_obj_fetch(struct ckpt_ctx *ctx, int objref,
 			    enum obj_type type);
 extern int ckpt_obj_lookup_add(struct ckpt_ctx *ctx, void *ptr,
 			       enum obj_type type, int *first);
+extern int obj_lookup(struct ckpt_ctx *ctx, void *ptr);
 extern int ckpt_obj_insert(struct ckpt_ctx *ctx, void *ptr, int objref,
 			   enum obj_type type);
 
@@ -95,6 +96,16 @@ static inline int restore_ipc_ns(struct ckpt_ctx *ctx)
 extern int checkpoint_ipcns(struct ckpt_ctx *ctx, struct ipc_namespace *ipc_ns);
 extern int restore_ipcns(struct ckpt_ctx *ctx);
 
+/* credentials */
+int checkpoint_groupinfo(struct ckpt_ctx *ctx, void *ptr);
+int checkpoint_userns(struct ckpt_ctx *ctx, void *ptr);
+int checkpoint_user(struct ckpt_ctx *ctx, void *ptr);
+int checkpoint_cred(struct ckpt_ctx *ctx, void *ptr);
+void *restore_groupinfo(struct ckpt_ctx *ctx);
+void *restore_userns(struct ckpt_ctx *ctx);
+void *restore_user(struct ckpt_ctx *ctx);
+void *restore_cred(struct ckpt_ctx *ctx);
+
 /* memory */
 extern void ckpt_pgarr_free(struct ckpt_ctx *ctx);
 
include/linux/checkpoint_hdr.h +67 -0

97 lines not on this page · raw message

include/linux/checkpoint_types.h +2 -0
index 3fdc43e..62b8272 100644
@@ -13,6 +13,7 @@
 #define CKPT_VERSION  1
 
 #define CHECKPOINT_SUBTREE	0x4
+#define RESTORE_CREATE_USERNS	0x8
 
 
 #ifdef __KERNEL__
@@ -67,6 +68,7 @@ struct ckpt_ctx {
 	atomic_t tasks_count;		/* sync of tasks: used to coordinate */
 	struct completion complete;	/* container root and other tasks on */
 	wait_queue_head_t waitq;	/* start, end, and restart ordering */
+	struct cred *realcred, *ecred;	/* tmp storage for cred at restart */
 };
 
 
include/linux/cred.h +11 -0
index bc5ffc2..14d7a10 100644
@@ -76,6 +76,12 @@ extern int groups_search(const struct group_info *, gid_t);
 extern int in_group_p(gid_t);
 extern int in_egroup_p(gid_t);
 
+#ifdef CONFIG_CHECKPOINT
+struct ckpt_ctx;
+int checkpoint_write_groupinfo(struct ckpt_ctx *, struct group_info *);
+struct group_info *restore_read_groupinfo(struct ckpt_ctx *);
+#endif
+
 /*
  * The common credentials for a thread group
  * - shared by CLONE_THREAD
@@ -351,4 +357,9 @@ int cred_setresgid(struct cred *new, gid_t rgid, gid_t egid, gid_t sgid);
 int cred_setfsuid(struct cred *new, uid_t uid, uid_t *old_fsuid);
 int cred_setfsgid(struct cred *new, gid_t gid, gid_t *old_fsgid);
 
+#ifdef CONFIG_CHECKPOINT
+int checkpoint_write_cred(struct ckpt_ctx *, const struct cred *);
+struct cred *restore_read_cred(struct ckpt_ctx *);
+#endif
+
 #endif /* _LINUX_CRED_H */
include/linux/sched.h +6 -0
index d057e7a..06445c8 100644
@@ -1871,6 +1871,12 @@ static inline struct user_struct *get_uid(struct user_struct *u)
 extern void free_uid(struct user_struct *);
 extern void release_uids(struct user_namespace *ns);
 
+#ifdef CONFIG_CHECKPOINT
+struct ckpt_ctx;
+int checkpoint_write_user(struct ckpt_ctx *, struct user_struct *);
+struct user_struct *restore_read_user(struct ckpt_ctx *);
+#endif
+
 #include <asm/current.h>
 
 extern void do_timer(unsigned long ticks);
include/linux/user_namespace.h +6 -0
index a2b82d5..3eeee40 100644
@@ -62,4 +62,10 @@ static inline void put_user_ns(struct user_namespace *ns)
 
 #endif
 
+#ifdef CONFIG_CHECKPOINT
+struct ckpt_ctx;
+int checkpoint_write_userns(struct ckpt_ctx *, struct user_namespace *);
+struct user_namespace *restore_read_userns(struct ckpt_ctx *);
+#endif
+
 #endif /* _LINUX_USER_H */
kernel/cred.c +120 -0

129 lines not on this page · raw message

kernel/groups.c +56 -0

65 lines not on this page · raw message

kernel/user.c +147 -0

156 lines not on this page · raw message

kernel/user_namespace.c +86 -0

95 lines not on this page · raw message

Signature · 1 line
1.6.1
#9 lore raw

Re: [PATCH 6/8] cr: checkpoint and restore task credentials

include/linux/checkpoint_hdr.h struct ckpt_hdr_task { · line 192

Quoted 3 lines · Serge E. Hallyn
include/linux/checkpoint_hdr.h +3 -0
+struct ckpt_hdr_cred {
+	struct ckpt_hdr h;
+	__u32 version; /* especially since capability sets might grow */

Oh, no. Image version should be incremented.

Quoted 21 lines
include/linux/checkpoint_hdr.h +17 -0
+	__u32 uid, suid, euid, fsuid;
+	__u32 gid, sgid, egid, fsgid;
+	__u64 cap_i, cap_p, cap_e;
+	__u64 cap_x;  /* bounding set ('X') */
+	__s32 user_ref;
+	__s32 groupinfo_ref;
+	__u32 padding;
+} __attribute__((aligned(8)));
+
+struct ckpt_hdr_groupinfo {
+	struct ckpt_hdr h;
+	__u32 ngroups;
+	/*
+	 * This is followed by ngroups __u32s
+	 */
+	__u32 groups[0];
+} __attribute__((aligned(8)));

include/linux/sched.h static inline struct user_struct *get_uid(struct user_struct *u) · line 1878

Quoted 8 lines
include/linux/sched.h +5 -0
@@ -1871,6 +1871,12 @@ static inline struct user_struct *get_uid(struct user_struct *u)
 extern void free_uid(struct user_struct *);
 extern void release_uids(struct user_namespace *ns);
 
+#ifdef CONFIG_CHECKPOINT
+struct ckpt_ctx;
+int checkpoint_write_user(struct ckpt_ctx *, struct user_struct *);
+struct user_struct *restore_read_user(struct ckpt_ctx *);
+#endif

I'll rip credential stuff from sched.h, better not add more.

Quoted 49 lines
kernel/groups.c +43 -0
@@ -287,3 +288,58 @@ int in_egroup_p(gid_t grp)
 }
 
 EXPORT_SYMBOL(in_egroup_p);
+
+#ifdef CONFIG_CHECKPOINT
+int checkpoint_write_groupinfo(struct ckpt_ctx *ctx, struct group_info *g)
+{
+	int ret, i, size;
+	struct ckpt_hdr_groupinfo *h;
+
+	size = sizeof(*h) + g->ngroups * sizeof(__u32);
+	h = ckpt_hdr_get_type(ctx, size, CKPT_HDR_GROUPINFO);
+	if (!h)
+		return -ENOMEM;
+
+	h->ngroups = g->ngroups;
+	for (i = 0; i < g->ngroups; i++)
+		h->groups[i] = GROUP_AT(g, i);
+
+	ret = ckpt_write_obj(ctx, (struct ckpt_hdr *) h);
+	ckpt_hdr_put(ctx, h);
+
+	return ret;
+}
+
+/*
+ * TODO - switch to reading in blocks, and only return an
+ * error for truly obscene # groups (like 10000)
+ */
+#define CKPT_MAXGROUPS 100
+#define MAX_GROUPINFO_SIZE (sizeof(*h)+CKPT_MAXGROUPS*sizeof(gid_t))
+struct group_info *restore_read_groupinfo(struct ckpt_ctx *ctx)
+{
+	struct group_info *g;
+	struct ckpt_hdr_groupinfo *h;
+	int i;
+
+	h = ckpt_read_buf_type(ctx, MAX_GROUPINFO_SIZE, CKPT_HDR_GROUPINFO);
+	if (IS_ERR(h))
+		return ERR_PTR(PTR_ERR(h));
+	if (h->ngroups > CKPT_MAXGROUPS) {
+		g = ERR_PTR(-EINVAL);
+		goto out;
+	}
+	g = groups_alloc(h->ngroups);
+	if (!g) {

kernel/groups.c int in_egroup_p(gid_t grp) · line 343

Quoted 10 lines
kernel/groups.c +10 -0
+		g = ERR_PTR(-ENOMEM);
+		goto out;
+	}
+	for (i = 0; i < h->ngroups; i++)
+		GROUP_AT(g, i) = h->groups[i];
+
+out:
+	ckpt_hdr_put(ctx, h);
+	return g;
+}

No checks, that groups in image are a) sorted, b) ->ngroups is compatible with object image.

maintainer
#10 lore raw

Re: [PATCH 6/8] cr: checkpoint and restore task credentials

Quoted 6 lines · Alexey Dobriyan

include/linux/checkpoint_hdr.h struct ckpt_hdr_task { · line 192

Quoted 3 lines · Serge E. Hallyn
include/linux/checkpoint_hdr.h +3 -0
+struct ckpt_hdr_cred {
+	struct ckpt_hdr h;
+	__u32 version; /* especially since capability sets might grow */

Oh, no. Image version should be incremented.

Why? The format hasn't changed since my last set I don't think...

Oh, I added the padding. Thanks. I have to bump it again for the next set (hopefully out today or tomorrow) as it adds securebits. (And hopefully a first stab at LSM, though it's not looking likely)

Quoted 24 lines
Quoted 24 lines
include/linux/checkpoint_hdr.h +17 -0
+	__u32 uid, suid, euid, fsuid;
+	__u32 gid, sgid, egid, fsgid;
+	__u64 cap_i, cap_p, cap_e;
+	__u64 cap_x;  /* bounding set ('X') */
+	__s32 user_ref;
+	__s32 groupinfo_ref;
+	__u32 padding;
+} __attribute__((aligned(8)));
+
+struct ckpt_hdr_groupinfo {
+	struct ckpt_hdr h;
+	__u32 ngroups;
+	/*
+	 * This is followed by ngroups __u32s
+	 */
+	__u32 groups[0];
+} __attribute__((aligned(8)));
include/linux/sched.h +0 -0
@@ -1871,6 +1871,12 @@ static inline struct user_struct *get_uid(struct user_struct *u)
 extern void free_uid(struct user_struct *);
 extern void release_uids(struct user_namespace *ns);
 
Quoted 7 lines

include/linux/sched.h static inline struct user_struct *get_uid(struct user_struct *u) · line 1878

Quoted 5 lines
include/linux/sched.h +5 -0
+#ifdef CONFIG_CHECKPOINT
+struct ckpt_ctx;
+int checkpoint_write_user(struct ckpt_ctx *, struct user_struct *);
+struct user_struct *restore_read_user(struct ckpt_ctx *);
+#endif

I'll rip credential stuff from sched.h, better not add more.

Yeah I'll move this in cred.h.

...

Quoted 22 lines
Quoted 22 lines
include/linux/sched.h +22 -0
+#define CKPT_MAXGROUPS 100
+#define MAX_GROUPINFO_SIZE (sizeof(*h)+CKPT_MAXGROUPS*sizeof(gid_t))
+struct group_info *restore_read_groupinfo(struct ckpt_ctx *ctx)
+{
+	struct group_info *g;
+	struct ckpt_hdr_groupinfo *h;
+	int i;
+
+	h = ckpt_read_buf_type(ctx, MAX_GROUPINFO_SIZE, CKPT_HDR_GROUPINFO);
+	if (IS_ERR(h))
+		return ERR_PTR(PTR_ERR(h));
+	if (h->ngroups > CKPT_MAXGROUPS) {
+		g = ERR_PTR(-EINVAL);
+		goto out;
+	}
+	g = groups_alloc(h->ngroups);
+	if (!g) {
+		g = ERR_PTR(-ENOMEM);
+		goto out;
+	}
+	for (i = 0; i < h->ngroups; i++)
+		GROUP_AT(g, i) = h->groups[i];
Quoted 8 lines

include/linux/sched.h

Quoted 5 lines
include/linux/sched.h +5 -0
+
+out:
+	ckpt_hdr_put(ctx, h);
+	return g;
+}

No checks, that groups in image are a) sorted, b) ->ngroups is compatible with object image.

Thanks, will fix.

So I'd like to suggest that we take the pieces that we can both use (the code in groups.c, cred.c, security/security.c, and capabilities) and get it identical between both versions. But we would need to find a way to ignore API differences for reading and writing the checkpoint file.

BTW I have some credentials (users, user namespaces, and securebits) testcases under cr_tests/userns/ in git://git.sr71.net/~hallyn/cr_tests.git.
Maybe you can reuse some of that for your own testing.

thanks,
-serge

#11 lore raw

Re: [PATCH 6/8] cr: checkpoint and restore task credentials

Quoted 8 lines · Serge E. Hallyn
Quoted 6 lines · Alexey Dobriyan

include/linux/checkpoint_hdr.h struct ckpt_hdr_task { · line 192

Quoted 3 lines · Serge E. Hallyn
include/linux/checkpoint_hdr.h +3 -0
+struct ckpt_hdr_cred {
+	struct ckpt_hdr h;
+	__u32 version; /* especially since capability sets might grow */

Oh, no. Image version should be incremented.

Quoted 6 lines

Why? The format hasn't changed since my last set I don't think...

Oh, I added the padding. Thanks. I have to bump it again for the next set (hopefully out today or tomorrow) as it adds securebits. (And hopefully a first stab at LSM, though it's not looking likely)

Well, formally format has changed.

But the statement is that image version alone is enough so per-object image versions aren't necessary.

#12 lore raw

PATCH 7/8 cr: restore file->f_cred

In reply to Serge E. Hallyn

Restore a file's f_cred. This is set to the cred of the task doing the open, so often it will be the same as that of the restarted task.

---
 checkpoint/files.c             |   16 ++++++++++++++--
 include/linux/checkpoint_hdr.h |    2 +-
 2 files changed, 15 insertions(+), 3 deletions(-)
checkpoint/files.c +14 -2
index 8c7ba9f..3e9ff75 100644
@@ -152,7 +152,11 @@ int checkpoint_file_common(struct ckpt_ctx *ctx, struct file *file,
 	h->f_pos = file->f_pos;
 	h->f_version = file->f_version;
 
-	/* FIX: need also file->uid, file->gid, file->f_owner, etc */
+	h->f_credref = checkpoint_obj(ctx, file->f_cred, CKPT_OBJ_CRED);
+	if (h->f_credref < 0)
+		return h->f_credref;
+
+	/* FIX: need also file->f_owner, etc */
 
 	return 0;
 }
@@ -361,8 +365,16 @@ int restore_file_common(struct ckpt_ctx *ctx, struct file *file,
 			struct ckpt_hdr_file *h)
 {
 	int ret;
+	struct cred *cred;
+
+	/* FIX: need to restore owner etc */
 
-	/* FIX: need to restore uid, gid, owner etc */
+	/* restore the cred */
+	cred = ckpt_obj_fetch(ctx, h->f_credref, CKPT_OBJ_CRED);
+	if (IS_ERR(cred))
+		return PTR_ERR(cred);
+	put_cred(file->f_cred);
+	file->f_cred = get_cred(cred);
 
 	/* safe to set 1st arg (fd) to 0, as command is F_SETFL */
 	ret = vfs_fcntl(0, F_SETFL, h->f_flags & CKPT_SETFL_MASK, file);
include/linux/checkpoint_hdr.h +1 -1
index df35703..0bad447 100644
@@ -350,7 +350,7 @@ struct ckpt_hdr_file {
 	__u32 f_type;
 	__u32 f_mode;
 	__u32 f_flags;
-	__u32 _padding;
+	__s32 f_credref;
 	__u64 f_pos;
 	__u64 f_version;
 } __attribute__((aligned(8)));
Signature · 1 line
1.6.1
#13 lore raw

PATCH 8/8 user namespaces: debug refcounts

In reply to Serge E. Hallyn

Create /proc/userns, which prints out all user namespaces. It prints the address of the user_ns itself, the uid and userns address of the user who created it, and the reference count.

---
 checkpoint/process.c           |    2 -
 include/linux/user_namespace.h |    2 +
 kernel/user.c                  |    1 +
 kernel/user_namespace.c        |   84 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+), 2 deletions(-)
checkpoint/process.c +0 -2
index 41656e3..c1db231 100644
@@ -800,9 +800,7 @@ static int restore_creds(struct ckpt_ctx *ctx)
 int restore_task(struct ckpt_ctx *ctx)
 {
 	int ret;
-	struct cred *realcred, *ecred;
 
-	ctx->realcred = ctx->ecred = NULL;
 	ret = restore_task_struct(ctx);
 	ckpt_debug("ret %d\n", ret);
 	if (ret < 0)
include/linux/user_namespace.h +2 -0
index 3eeee40..4503224 100644
@@ -14,8 +14,10 @@ struct user_namespace {
 	struct hlist_head	uidhash_table[UIDHASH_SZ];
 	struct user_struct	*creator;
 	struct work_struct	destroyer;
+	struct list_head	list;
 };
 
+extern spinlock_t usernslist_lock;
 extern struct user_namespace init_user_ns;
 
 #ifdef CONFIG_USER_NS
kernel/user.c +1 -0
index 97f13e2..1a9a44f 100644
@@ -24,6 +24,7 @@ struct user_namespace init_user_ns = {
 		.refcount	= ATOMIC_INIT(2),
 	},
 	.creator = &root_user,
+	.list = LIST_HEAD_INIT(init_user_ns.list),
 };
 EXPORT_SYMBOL_GPL(init_user_ns);
 
kernel/user_namespace.c +84 -0

108 lines not on this page · raw message

Signature · 1 line
1.6.1
#14 lore raw
In reply to Serge E. Hallyn
Quoted 7 lines · Serge E. Hallyn

Following is the next version of the credentials c/r patchset, on top of the c/r patchset at
git://git.ncl.cs.columbia.edu/pub/git/linux-cr.git

It implements checkpoint and restart of user, user namespaces, groups, supplementary groups, and struct cred.

Quoted 7 lines

There is a question as to what to do about LSM data at restart. Right now I'm ignoring it, which means that prepare_creds() should ensure that the restart tasks get the context of the task calling sys_restart(). I suspect the right thing to do is to add two new LSM hooks, one which checks current's authorization to restart from the checkpoint file,

How would that work? Based on information in the file? You have to assume that some number of checkpoint files have been hand written by Elbonian ne'er do wells.

Quoted 6 lines

and one which determines
the task->cred->security filed based upon any of:
1. current_security() of the task calling sys_restart()
2. the task->cred->security checkpointed in the ckpt file
3. the ->security of the checkpoint file

For Smack the correct behavior would be:

1. for sys_restart() callers without CAP_MAC_ADMIN
2. for sys_restart() callers with CAP_MAC_ADMIN
3. never

sys_restart() callers running with CAP_MAC_ADMIN would have to be very very careful about the files they restart. But that's nothing new in the MAC world.

Quoted 12 lines

Oren, I think this version has all the changes you asked for except for restoring cred info for sysvipc.

thanks,
-serge
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#15 lore raw
Quoted 16 lines · Casey Schaufler
Quoted 7 lines · Serge E. Hallyn

Following is the next version of the credentials c/r patchset, on top of the c/r patchset at
git://git.ncl.cs.columbia.edu/pub/git/linux-cr.git

It implements checkpoint and restart of user, user namespaces, groups, supplementary groups, and struct cred.

Quoted 7 lines

There is a question as to what to do about LSM data at restart. Right now I'm ignoring it, which means that prepare_creds() should ensure that the restart tasks get the context of the task calling sys_restart(). I suspect the right thing to do is to add two new LSM hooks, one which checks current's authorization to restart from the checkpoint file,

Quoted 3 lines

How would that work? Based on information in the file? You have to assume that some number of checkpoint files have been hand written by Elbonian ne'er do wells.

Not based on information in the file, but based on the credentials of the task which created the file, and whether an unprivileged task could have hand-edited the file before feeding it to sys_restart().

So some example decisions in terms of selinux contexts might be,
1. a task of user_u may restart a file of type user_u if the checkpointed context is user_u
2. a task of user_u may NOT restart a file of type user_u if the checkpointed context is root_u
3. a task of root_u may restart a file of type root_u if the checkpointed context is user_u

Uh, so yes, bsaed on info in the file as well :) Except of course the LSM would just be fed the checkpointed context and the checkpoint file context (and can deduce current's context).

Quoted 7 lines
Quoted 6 lines

and one which determines
the task->cred->security filed based upon any of:
1. current_security() of the task calling sys_restart()
2. the task->cred->security checkpointed in the ckpt file
3. the ->security of the checkpoint file

Quoted 5 lines

For Smack the correct behavior would be:

1. for sys_restart() callers without CAP_MAC_ADMIN
2. for sys_restart() callers with CAP_MAC_ADMIN
3. never

That makes sense, and is basically analagous (if I'm thinking right) to how I'm doing capabilities.

So the first (authorization hook) for smack would just always return TRUE?

I can hook that up right now...

Quoted 3 lines

sys_restart() callers running with CAP_MAC_ADMIN would have to be very very careful about the files they restart. But that's nothing new in the MAC world.

Yup.

Mind you eventually I expect a setup where some privileged program is asked (by privileged or unprivilegd tasks) to create a checkpoint and ask the TPM to sign it. No unprivileged program can sign an image directly, so then a restart of a task with privilege can be restricted to anything with a valid signature. In that case, it may be safe to have the checkpointed task's credentials completely restored, including LSM labels.

But that's a ways off.

thanks,
-serge

#16 lore raw
Quoted 37 lines · Serge E. Hallyn
Quoted 17 lines · Casey Schaufler
Quoted 7 lines · Serge E. Hallyn

Following is the next version of the credentials c/r patchset, on top of the c/r patchset at
git://git.ncl.cs.columbia.edu/pub/git/linux-cr.git

It implements checkpoint and restart of user, user namespaces, groups, supplementary groups, and struct cred.

Quoted 8 lines

There is a question as to what to do about LSM data at restart. Right now I'm ignoring it, which means that prepare_creds() should ensure that the restart tasks get the context of the task calling sys_restart(). I suspect the right thing to do is to add two new LSM hooks, one which checks current's authorization to restart from the checkpoint file,

Quoted 4 lines

How would that work? Based on information in the file? You have to assume that some number of checkpoint files have been hand written by Elbonian ne'er do wells.

Not based on information in the file, but based on the credentials of the task which created the file, and whether an unprivileged task could have hand-edited the file before feeding it to sys_restart().

So some example decisions in terms of selinux contexts might be,
1. a task of user_u may restart a file of type user_u if the checkpointed context is user_u
2. a task of user_u may NOT restart a file of type user_u if the checkpointed context is root_u
3. a task of root_u may restart a file of type root_u if the checkpointed context is user_u

Quoted 4 lines

Uh, so yes, bsaed on info in the file as well :) Except of course the LSM would just be fed the checkpointed context and the checkpoint file context (and can deduce current's context).

And SELinux can do whatever calculations it likes based on the three contexts and the loaded policy. Are you at all concerned about the possibility that the policy may have changed? I can envision scenarios in which it would be impossible for a process to gain a particular context under current policy, but that a checkpointed process may have stored away.

Quoted 15 lines
Quoted 8 lines
Quoted 8 lines

and one which determines
the task->cred->security filed based upon any of:
1. current_security() of the task calling sys_restart()
2. the task->cred->security checkpointed in the ckpt file
3. the ->security of the checkpoint file

Quoted 6 lines

For Smack the correct behavior would be:

1. for sys_restart() callers without CAP_MAC_ADMIN
2. for sys_restart() callers with CAP_MAC_ADMIN
3. never

Quoted 6 lines

That makes sense, and is basically analagous (if I'm thinking right) to how I'm doing capabilities.

So the first (authorization hook) for smack would just always return TRUE?

I suggest that it needs to check for a valid Smack label. Even though they're just text strings they do have limitations, including size (> 0 < 24) and character set. A call to smk_import() is the right way to do it, as it also makes sure the label is in the internal list. If smk_import() returns NULL something's amiss.

Quoted 2 lines

I can hook that up right now...

I bet you could do it even with the call to smk_import. (smiley here)

Quoted 8 lines
Quoted 5 lines

sys_restart() callers running with CAP_MAC_ADMIN would have to be very very careful about the files they restart. But that's nothing new in the MAC world.

Yup.

Quoted 8 lines

Mind you eventually I expect a setup where some privileged program is asked (by privileged or unprivilegd tasks) to create a checkpoint and ask the TPM to sign it. No unprivileged program can sign an image directly, so then a restart of a task with privilege can be restricted to anything with a valid signature. In that case, it may be safe to have the checkpointed task's credentials completely restored, including LSM labels.

All of the current LSMs share the property that the access control rules (SELinux policy, Smack access rules, TOMOYO policy) may change between the time of checkpoint and the time of restart. If I had a silver bullet answer to the concerns that raises I'd pass it along, but as I don't I'll stick to the answer I have for Smack (The rules of the moment are those that matter, and the architecture of Smack supports that) and leave the other LSMs to their own devices.

Quoted 2 lines

But that's a ways off.

It does look like a bit of work.

Thank you.

Quoted 9 lines

thanks,
-serge
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#17 lore raw
Quoted 2 lines · Casey Schaufler
Quoted 1 line · Serge E. Hallyn

Quoting Casey Schaufler ([email protected]):

...

Quoted 11 lines
Quoted 4 lines

Uh, so yes, bsaed on info in the file as well :) Except of course the LSM would just be fed the checkpointed context and the checkpoint file context (and can deduce current's context).

And SELinux can do whatever calculations it likes based on the three contexts and the loaded policy. Are you at all concerned about the possibility that the policy may have changed? I can envision scenarios in which it would be impossible for a process to gain a particular context under current policy, but that a checkpointed process may have stored away.

Good point. But on the other hand, if the program were running the whole time, instead of being checkpointed and restarted, then the running program wouldn't be relabeled when the policy changed, right? Now if the domain becomes invalid, then presumably the restart would fail. But if the (source_domain,entry_type)->new_domain set changes from (root_t,x_entry_t)->x_t to (root_t,x_entry_t)->y_t, a task running as x_t won't be relabeled to y_t. So I don't thnk restarting a task which is checkpointed as x_t, under the x_t domain, is wrong.

Quoted 21 lines
Quoted 14 lines
Quoted 7 lines
Quoted 7 lines

and one which determines
the task->cred->security filed based upon any of:
1. current_security() of the task calling sys_restart()
2. the task->cred->security checkpointed in the ckpt file
3. the ->security of the checkpoint file

Quoted 6 lines

For Smack the correct behavior would be:

1. for sys_restart() callers without CAP_MAC_ADMIN
2. for sys_restart() callers with CAP_MAC_ADMIN
3. never

Quoted 6 lines

That makes sense, and is basically analagous (if I'm thinking right) to how I'm doing capabilities.

So the first (authorization hook) for smack would just always return TRUE?

Quoted 5 lines

I suggest that it needs to check for a valid Smack label. Even though they're just text strings they do have limitations, including size (> 0 < 24) and character set. A call to smk_import() is the right way to do it, as it also makes sure the label is in the internal list. If smk_import() returns NULL something's amiss.

Ok, thanks.

-serge