Skip to content

Commit

Permalink
userns: Free user namespaces in process context
Browse files Browse the repository at this point in the history
Add the necessary boiler plate to move freeing of user namespaces into
work queue and thus into process context where things can sleep.

This is a necessary precursor to per user namespace sysctls.

Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
  • Loading branch information
ebiederm committed Aug 8, 2016
1 parent 13bcc6a commit b032132
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 3 additions & 2 deletions include/linux/user_namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct user_namespace {
struct key *persistent_keyring_register;
struct rw_semaphore persistent_keyring_register_sem;
#endif
struct work_struct work;
};

extern struct user_namespace init_user_ns;
Expand All @@ -54,12 +55,12 @@ static inline struct user_namespace *get_user_ns(struct user_namespace *ns)

extern int create_user_ns(struct cred *new);
extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
extern void free_user_ns(struct user_namespace *ns);
extern void __put_user_ns(struct user_namespace *ns);

static inline void put_user_ns(struct user_namespace *ns)
{
if (ns && atomic_dec_and_test(&ns->count))
free_user_ns(ns);
__put_user_ns(ns);
}

struct seq_operations;
Expand Down
14 changes: 11 additions & 3 deletions kernel/user_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static DEFINE_MUTEX(userns_state_mutex);
static bool new_idmap_permitted(const struct file *file,
struct user_namespace *ns, int cap_setid,
struct uid_gid_map *map);
static void free_user_ns(struct work_struct *work);

static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
{
Expand Down Expand Up @@ -101,6 +102,7 @@ int create_user_ns(struct cred *new)
ns->level = parent_ns->level + 1;
ns->owner = owner;
ns->group = group;
INIT_WORK(&ns->work, free_user_ns);

/* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
mutex_lock(&userns_state_mutex);
Expand Down Expand Up @@ -135,9 +137,10 @@ int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
return err;
}

void free_user_ns(struct user_namespace *ns)
static void free_user_ns(struct work_struct *work)
{
struct user_namespace *parent;
struct user_namespace *parent, *ns =
container_of(work, struct user_namespace, work);

do {
parent = ns->parent;
Expand All @@ -149,7 +152,12 @@ void free_user_ns(struct user_namespace *ns)
ns = parent;
} while (atomic_dec_and_test(&parent->count));
}
EXPORT_SYMBOL(free_user_ns);

void __put_user_ns(struct user_namespace *ns)
{
schedule_work(&ns->work);
}
EXPORT_SYMBOL(__put_user_ns);

static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
{
Expand Down

0 comments on commit b032132

Please sign in to comment.