174bd59bbSPavel Emelyanov /* 274bd59bbSPavel Emelyanov * Pid namespaces 374bd59bbSPavel Emelyanov * 474bd59bbSPavel Emelyanov * Authors: 574bd59bbSPavel Emelyanov * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc. 674bd59bbSPavel Emelyanov * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM 774bd59bbSPavel Emelyanov * Many thanks to Oleg Nesterov for comments and help 874bd59bbSPavel Emelyanov * 974bd59bbSPavel Emelyanov */ 1074bd59bbSPavel Emelyanov 1174bd59bbSPavel Emelyanov #include <linux/pid.h> 1274bd59bbSPavel Emelyanov #include <linux/pid_namespace.h> 1374bd59bbSPavel Emelyanov #include <linux/syscalls.h> 1474bd59bbSPavel Emelyanov #include <linux/err.h> 150b6b030fSPavel Emelyanov #include <linux/acct.h> 165a0e3ad6STejun Heo #include <linux/slab.h> 174308eebbSEric W. Biederman #include <linux/proc_fs.h> 1874bd59bbSPavel Emelyanov 1974bd59bbSPavel Emelyanov #define BITS_PER_PAGE (PAGE_SIZE*8) 2074bd59bbSPavel Emelyanov 2174bd59bbSPavel Emelyanov struct pid_cache { 2274bd59bbSPavel Emelyanov int nr_ids; 2374bd59bbSPavel Emelyanov char name[16]; 2474bd59bbSPavel Emelyanov struct kmem_cache *cachep; 2574bd59bbSPavel Emelyanov struct list_head list; 2674bd59bbSPavel Emelyanov }; 2774bd59bbSPavel Emelyanov 2874bd59bbSPavel Emelyanov static LIST_HEAD(pid_caches_lh); 2974bd59bbSPavel Emelyanov static DEFINE_MUTEX(pid_caches_mutex); 3074bd59bbSPavel Emelyanov static struct kmem_cache *pid_ns_cachep; 3174bd59bbSPavel Emelyanov 3274bd59bbSPavel Emelyanov /* 3374bd59bbSPavel Emelyanov * creates the kmem cache to allocate pids from. 3474bd59bbSPavel Emelyanov * @nr_ids: the number of numerical ids this pid will have to carry 3574bd59bbSPavel Emelyanov */ 3674bd59bbSPavel Emelyanov 3774bd59bbSPavel Emelyanov static struct kmem_cache *create_pid_cachep(int nr_ids) 3874bd59bbSPavel Emelyanov { 3974bd59bbSPavel Emelyanov struct pid_cache *pcache; 4074bd59bbSPavel Emelyanov struct kmem_cache *cachep; 4174bd59bbSPavel Emelyanov 4274bd59bbSPavel Emelyanov mutex_lock(&pid_caches_mutex); 4374bd59bbSPavel Emelyanov list_for_each_entry(pcache, &pid_caches_lh, list) 4474bd59bbSPavel Emelyanov if (pcache->nr_ids == nr_ids) 4574bd59bbSPavel Emelyanov goto out; 4674bd59bbSPavel Emelyanov 4774bd59bbSPavel Emelyanov pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL); 4874bd59bbSPavel Emelyanov if (pcache == NULL) 4974bd59bbSPavel Emelyanov goto err_alloc; 5074bd59bbSPavel Emelyanov 5174bd59bbSPavel Emelyanov snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids); 5274bd59bbSPavel Emelyanov cachep = kmem_cache_create(pcache->name, 5374bd59bbSPavel Emelyanov sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid), 5474bd59bbSPavel Emelyanov 0, SLAB_HWCACHE_ALIGN, NULL); 5574bd59bbSPavel Emelyanov if (cachep == NULL) 5674bd59bbSPavel Emelyanov goto err_cachep; 5774bd59bbSPavel Emelyanov 5874bd59bbSPavel Emelyanov pcache->nr_ids = nr_ids; 5974bd59bbSPavel Emelyanov pcache->cachep = cachep; 6074bd59bbSPavel Emelyanov list_add(&pcache->list, &pid_caches_lh); 6174bd59bbSPavel Emelyanov out: 6274bd59bbSPavel Emelyanov mutex_unlock(&pid_caches_mutex); 6374bd59bbSPavel Emelyanov return pcache->cachep; 6474bd59bbSPavel Emelyanov 6574bd59bbSPavel Emelyanov err_cachep: 6674bd59bbSPavel Emelyanov kfree(pcache); 6774bd59bbSPavel Emelyanov err_alloc: 6874bd59bbSPavel Emelyanov mutex_unlock(&pid_caches_mutex); 6974bd59bbSPavel Emelyanov return NULL; 7074bd59bbSPavel Emelyanov } 7174bd59bbSPavel Emelyanov 72ed469a63SAlexey Dobriyan static struct pid_namespace *create_pid_namespace(struct pid_namespace *parent_pid_ns) 7374bd59bbSPavel Emelyanov { 7474bd59bbSPavel Emelyanov struct pid_namespace *ns; 75ed469a63SAlexey Dobriyan unsigned int level = parent_pid_ns->level + 1; 764308eebbSEric W. Biederman int i, err = -ENOMEM; 7774bd59bbSPavel Emelyanov 7884406c15SPavel Emelyanov ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL); 7974bd59bbSPavel Emelyanov if (ns == NULL) 8074bd59bbSPavel Emelyanov goto out; 8174bd59bbSPavel Emelyanov 8274bd59bbSPavel Emelyanov ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL); 8374bd59bbSPavel Emelyanov if (!ns->pidmap[0].page) 8474bd59bbSPavel Emelyanov goto out_free; 8574bd59bbSPavel Emelyanov 8674bd59bbSPavel Emelyanov ns->pid_cachep = create_pid_cachep(level + 1); 8774bd59bbSPavel Emelyanov if (ns->pid_cachep == NULL) 8874bd59bbSPavel Emelyanov goto out_free_map; 8974bd59bbSPavel Emelyanov 9074bd59bbSPavel Emelyanov kref_init(&ns->kref); 9174bd59bbSPavel Emelyanov ns->level = level; 92ed469a63SAlexey Dobriyan ns->parent = get_pid_ns(parent_pid_ns); 9374bd59bbSPavel Emelyanov 9474bd59bbSPavel Emelyanov set_bit(0, ns->pidmap[0].page); 9574bd59bbSPavel Emelyanov atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1); 9674bd59bbSPavel Emelyanov 9784406c15SPavel Emelyanov for (i = 1; i < PIDMAP_ENTRIES; i++) 9874bd59bbSPavel Emelyanov atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE); 9974bd59bbSPavel Emelyanov 1004308eebbSEric W. Biederman err = pid_ns_prepare_proc(ns); 1014308eebbSEric W. Biederman if (err) 1024308eebbSEric W. Biederman goto out_put_parent_pid_ns; 1034308eebbSEric W. Biederman 10474bd59bbSPavel Emelyanov return ns; 10574bd59bbSPavel Emelyanov 1064308eebbSEric W. Biederman out_put_parent_pid_ns: 1074308eebbSEric W. Biederman put_pid_ns(parent_pid_ns); 10874bd59bbSPavel Emelyanov out_free_map: 10974bd59bbSPavel Emelyanov kfree(ns->pidmap[0].page); 11074bd59bbSPavel Emelyanov out_free: 11174bd59bbSPavel Emelyanov kmem_cache_free(pid_ns_cachep, ns); 11274bd59bbSPavel Emelyanov out: 1134308eebbSEric W. Biederman return ERR_PTR(err); 11474bd59bbSPavel Emelyanov } 11574bd59bbSPavel Emelyanov 11674bd59bbSPavel Emelyanov static void destroy_pid_namespace(struct pid_namespace *ns) 11774bd59bbSPavel Emelyanov { 11874bd59bbSPavel Emelyanov int i; 11974bd59bbSPavel Emelyanov 12074bd59bbSPavel Emelyanov for (i = 0; i < PIDMAP_ENTRIES; i++) 12174bd59bbSPavel Emelyanov kfree(ns->pidmap[i].page); 12274bd59bbSPavel Emelyanov kmem_cache_free(pid_ns_cachep, ns); 12374bd59bbSPavel Emelyanov } 12474bd59bbSPavel Emelyanov 12574bd59bbSPavel Emelyanov struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns) 12674bd59bbSPavel Emelyanov { 12774bd59bbSPavel Emelyanov if (!(flags & CLONE_NEWPID)) 128dca4a979SAlexey Dobriyan return get_pid_ns(old_ns); 129e5a47386SSukadev Bhattiprolu if (flags & (CLONE_THREAD|CLONE_PARENT)) 130dca4a979SAlexey Dobriyan return ERR_PTR(-EINVAL); 131dca4a979SAlexey Dobriyan return create_pid_namespace(old_ns); 13274bd59bbSPavel Emelyanov } 13374bd59bbSPavel Emelyanov 13474bd59bbSPavel Emelyanov void free_pid_ns(struct kref *kref) 13574bd59bbSPavel Emelyanov { 13674bd59bbSPavel Emelyanov struct pid_namespace *ns, *parent; 13774bd59bbSPavel Emelyanov 13874bd59bbSPavel Emelyanov ns = container_of(kref, struct pid_namespace, kref); 13974bd59bbSPavel Emelyanov 14074bd59bbSPavel Emelyanov parent = ns->parent; 14174bd59bbSPavel Emelyanov destroy_pid_namespace(ns); 14274bd59bbSPavel Emelyanov 14374bd59bbSPavel Emelyanov if (parent != NULL) 14474bd59bbSPavel Emelyanov put_pid_ns(parent); 14574bd59bbSPavel Emelyanov } 14674bd59bbSPavel Emelyanov 14774bd59bbSPavel Emelyanov void zap_pid_ns_processes(struct pid_namespace *pid_ns) 14874bd59bbSPavel Emelyanov { 14974bd59bbSPavel Emelyanov int nr; 15074bd59bbSPavel Emelyanov int rc; 151e4da026fSSukadev Bhattiprolu struct task_struct *task; 15274bd59bbSPavel Emelyanov 15374bd59bbSPavel Emelyanov /* 15474bd59bbSPavel Emelyanov * The last thread in the cgroup-init thread group is terminating. 15574bd59bbSPavel Emelyanov * Find remaining pid_ts in the namespace, signal and wait for them 15674bd59bbSPavel Emelyanov * to exit. 15774bd59bbSPavel Emelyanov * 15874bd59bbSPavel Emelyanov * Note: This signals each threads in the namespace - even those that 15974bd59bbSPavel Emelyanov * belong to the same thread group, To avoid this, we would have 16074bd59bbSPavel Emelyanov * to walk the entire tasklist looking a processes in this 16174bd59bbSPavel Emelyanov * namespace, but that could be unnecessarily expensive if the 16274bd59bbSPavel Emelyanov * pid namespace has just a few processes. Or we need to 16374bd59bbSPavel Emelyanov * maintain a tasklist for each pid namespace. 16474bd59bbSPavel Emelyanov * 16574bd59bbSPavel Emelyanov */ 16674bd59bbSPavel Emelyanov read_lock(&tasklist_lock); 16774bd59bbSPavel Emelyanov nr = next_pidmap(pid_ns, 1); 16874bd59bbSPavel Emelyanov while (nr > 0) { 169e4da026fSSukadev Bhattiprolu rcu_read_lock(); 170e4da026fSSukadev Bhattiprolu 171e4da026fSSukadev Bhattiprolu task = pid_task(find_vpid(nr), PIDTYPE_PID); 172*a02d6fd6SOleg Nesterov if (task && !__fatal_signal_pending(task)) 173*a02d6fd6SOleg Nesterov send_sig_info(SIGKILL, SEND_SIG_FORCED, task); 174e4da026fSSukadev Bhattiprolu 175e4da026fSSukadev Bhattiprolu rcu_read_unlock(); 176e4da026fSSukadev Bhattiprolu 17774bd59bbSPavel Emelyanov nr = next_pidmap(pid_ns, nr); 17874bd59bbSPavel Emelyanov } 17974bd59bbSPavel Emelyanov read_unlock(&tasklist_lock); 18074bd59bbSPavel Emelyanov 18174bd59bbSPavel Emelyanov do { 18274bd59bbSPavel Emelyanov clear_thread_flag(TIF_SIGPENDING); 18374bd59bbSPavel Emelyanov rc = sys_wait4(-1, NULL, __WALL, NULL); 18474bd59bbSPavel Emelyanov } while (rc != -ECHILD); 18574bd59bbSPavel Emelyanov 1860b6b030fSPavel Emelyanov acct_exit_ns(pid_ns); 18774bd59bbSPavel Emelyanov return; 18874bd59bbSPavel Emelyanov } 18974bd59bbSPavel Emelyanov 190b8f566b0SPavel Emelyanov static int pid_ns_ctl_handler(struct ctl_table *table, int write, 191b8f566b0SPavel Emelyanov void __user *buffer, size_t *lenp, loff_t *ppos) 192b8f566b0SPavel Emelyanov { 193b8f566b0SPavel Emelyanov struct ctl_table tmp = *table; 194b8f566b0SPavel Emelyanov 195b8f566b0SPavel Emelyanov if (write && !capable(CAP_SYS_ADMIN)) 196b8f566b0SPavel Emelyanov return -EPERM; 197b8f566b0SPavel Emelyanov 198b8f566b0SPavel Emelyanov /* 199b8f566b0SPavel Emelyanov * Writing directly to ns' last_pid field is OK, since this field 200b8f566b0SPavel Emelyanov * is volatile in a living namespace anyway and a code writing to 201b8f566b0SPavel Emelyanov * it should synchronize its usage with external means. 202b8f566b0SPavel Emelyanov */ 203b8f566b0SPavel Emelyanov 204b8f566b0SPavel Emelyanov tmp.data = ¤t->nsproxy->pid_ns->last_pid; 205b8f566b0SPavel Emelyanov return proc_dointvec(&tmp, write, buffer, lenp, ppos); 206b8f566b0SPavel Emelyanov } 207b8f566b0SPavel Emelyanov 208b8f566b0SPavel Emelyanov static struct ctl_table pid_ns_ctl_table[] = { 209b8f566b0SPavel Emelyanov { 210b8f566b0SPavel Emelyanov .procname = "ns_last_pid", 211b8f566b0SPavel Emelyanov .maxlen = sizeof(int), 212b8f566b0SPavel Emelyanov .mode = 0666, /* permissions are checked in the handler */ 213b8f566b0SPavel Emelyanov .proc_handler = pid_ns_ctl_handler, 214b8f566b0SPavel Emelyanov }, 215b8f566b0SPavel Emelyanov { } 216b8f566b0SPavel Emelyanov }; 217b8f566b0SPavel Emelyanov 218b8f566b0SPavel Emelyanov static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } }; 219b8f566b0SPavel Emelyanov 22074bd59bbSPavel Emelyanov static __init int pid_namespaces_init(void) 22174bd59bbSPavel Emelyanov { 22274bd59bbSPavel Emelyanov pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC); 223b8f566b0SPavel Emelyanov register_sysctl_paths(kern_path, pid_ns_ctl_table); 22474bd59bbSPavel Emelyanov return 0; 22574bd59bbSPavel Emelyanov } 22674bd59bbSPavel Emelyanov 22774bd59bbSPavel Emelyanov __initcall(pid_namespaces_init); 228