3409 lines · 83.7 KB · C
RawDownload
	struct task_struct *p;	struct multiprocess_signals delayed;	struct file *pidfile = NULL;	const u64 clone_flags = args->flags;	struct nsproxy *nsp = current->nsproxy; 	/*	 * Don't allow sharing the root directory with processes in a different	 * namespace	 */	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))		return ERR_PTR(-EINVAL); 	if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))		return ERR_PTR(-EINVAL); 	/*	 * Thread groups must share signals as well, and detached threads	 * can only be started up within the thread group.	 */	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))		return ERR_PTR(-EINVAL); 	/*	 * Shared signal handlers imply shared VM. By way of the above,	 * thread groups also imply shared VM. Blocking this case allows	 * for various simplifications in other code.	 */	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))		return ERR_PTR(-EINVAL); 	/*	 * Siblings of global init remain as zombies on exit since they are	 * not reaped by their parent (swapper). To solve this and to avoid	 * multi-rooted process trees, prevent global and container-inits	 * from creating siblings.	 */	if ((clone_flags & CLONE_PARENT) &&				current->signal->flags & SIGNAL_UNKILLABLE)		return ERR_PTR(-EINVAL); 	/*	 * If the new process will be in a different pid or user namespace	 * do not allow it to share a thread group with the forking task.	 */	if (clone_flags & CLONE_THREAD) {		if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||		    (task_active_pid_ns(current) != nsp->pid_ns_for_children))			return ERR_PTR(-EINVAL);	} 	if (clone_flags & CLONE_PIDFD) {		/*		 * - CLONE_DETACHED is blocked so that we can potentially		 *   reuse it later for CLONE_PIDFD.		 */		if (clone_flags & CLONE_DETACHED)			return ERR_PTR(-EINVAL);	} 	if (clone_flags & CLONE_AUTOREAP) {		if (clone_flags & CLONE_THREAD)			return ERR_PTR(-EINVAL);		if (clone_flags & CLONE_PARENT)			return ERR_PTR(-EINVAL);		if (args->exit_signal)			return ERR_PTR(-EINVAL);	} 	if ((clone_flags & CLONE_PARENT) && current->signal->autoreap)		return ERR_PTR(-EINVAL); 	if (clone_flags & CLONE_NNP) {		if (clone_flags & CLONE_THREAD)			return ERR_PTR(-EINVAL);	} 	if (clone_flags & CLONE_PIDFD_AUTOKILL) {		if (!(clone_flags & CLONE_PIDFD))			return ERR_PTR(-EINVAL);		if (!(clone_flags & CLONE_AUTOREAP))			return ERR_PTR(-EINVAL);		if (clone_flags & CLONE_THREAD)			return ERR_PTR(-EINVAL);		/*		 * Without CLONE_NNP the child could escalate privileges		 * after being spawned, so require CAP_SYS_ADMIN.		 * With CLONE_NNP the child can't gain new privileges,		 * so allow unprivileged usage.		 */		if (!(clone_flags & CLONE_NNP) &&		    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))			return ERR_PTR(-EPERM);	} 	/*	 * Force any signals received before this point to be delivered	 * before the fork happens.  Collect up signals sent to multiple	 * processes that happen during the fork and delay them so that	 * they appear to happen after the fork.	 */	sigemptyset(&delayed.signal);	INIT_HLIST_NODE(&delayed.node); 	spin_lock_irq(&current->sighand->siglock);	if (!(clone_flags & CLONE_THREAD))		hlist_add_head(&delayed.node, &current->signal->multiprocess);	recalc_sigpending();	spin_unlock_irq(&current->sighand->siglock);	retval = -ERESTARTNOINTR;	if (task_sigpending(current))		goto fork_out; 	retval = -ENOMEM;	p = dup_task_struct(current, node);	if (!p)		goto fork_out;	retval = copy_exec_state(clone_flags, p);	if (retval)		goto bad_fork_free;	p->flags &= ~PF_KTHREAD;	if (args->kthread)		p->flags |= PF_KTHREAD;	if (args->user_worker) {		/*		 * Mark us a user worker, and block any signal that isn't		 * fatal or STOP		 */		p->flags |= PF_USER_WORKER;		siginitsetinv(&p->blocked, sigmask(SIGKILL)|sigmask(SIGSTOP));	}	if (args->io_thread)		p->flags |= PF_IO_WORKER; 	if (args->name)		strscpy_pad(p->comm, args->name, sizeof(p->comm)); 	p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL;	/*	 * TID is cleared in mm_release() when the task exits	 */	p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL; 	ftrace_graph_init_task(p); 	rt_mutex_init_task(p);	raw_spin_lock_init(&p->blocked_lock); 	lockdep_assert_irqs_enabled();#ifdef CONFIG_PROVE_LOCKING	DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);#endif	retval = copy_creds(p, clone_flags);	if (retval < 0)		goto bad_fork_free; 	retval = -EAGAIN;	if (is_rlimit_overlimit(task_ucounts(p), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) {		if (p->real_cred->user != INIT_USER &&		    !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))			goto bad_fork_cleanup_count;	}	current->flags &= ~PF_NPROC_EXCEEDED; 	/*	 * If multiple threads are within copy_process(), then this check	 * triggers too late. This doesn't hurt, the check is only there	 * to stop root fork bombs.	 */	retval = -EAGAIN;	if (data_race(nr_threads >= max_threads))		goto bad_fork_cleanup_count; 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */	p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE | PF_NO_SETAFFINITY);	p->flags |= PF_FORKNOEXEC;	INIT_LIST_HEAD(&p->children);	INIT_LIST_HEAD(&p->sibling);	rcu_copy_process(p);	p->vfork_done = NULL;	spin_lock_init(&p->alloc_lock); 	init_sigpending(&p->pending); 	p->utime = p->stime = p->gtime = 0;#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME	p->utimescaled = p->stimescaled = 0;#endif	prev_cputime_init(&p->prev_cputime); #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN	seqcount_init(&p->vtime.seqcount);	p->vtime.starttime = 0;	p->vtime.state = VTIME_INACTIVE;#endif #ifdef CONFIG_IO_URING	p->io_uring = NULL;	retval = io_uring_fork(p);	if (unlikely(retval))		goto bad_fork_cleanup_delayacct;	retval = -EAGAIN;#endif 	p->default_timer_slack_ns = current->timer_slack_ns; #ifdef CONFIG_PSI	p->psi_flags = 0;#endif 	task_io_accounting_init(&p->ioac);	acct_clear_integrals(p); 	posix_cputimers_init(&p->posix_cputimers);	tick_dep_init_task(p); 	p->io_context = NULL;	audit_set_context(p, NULL);	cgroup_fork(p);	if (args->kthread) {		if (!set_kthread_struct(p))			goto bad_fork_cleanup_delayacct;	}#ifdef CONFIG_NUMA	p->mempolicy = mpol_dup(p->mempolicy);	if (IS_ERR(p->mempolicy)) {		retval = PTR_ERR(p->mempolicy);		p->mempolicy = NULL;		goto bad_fork_cleanup_delayacct;	}#endif#ifdef CONFIG_CPUSETS	p->cpuset_mem_spread_rotor = NUMA_NO_NODE;	seqcount_spinlock_init(&p->mems_allowed_seq, &p->alloc_lock);#endif#ifdef CONFIG_TRACE_IRQFLAGS	memset(&p->irqtrace, 0, sizeof(p->irqtrace));	p->irqtrace.hardirq_disable_ip	= _THIS_IP_;	p->irqtrace.softirq_enable_ip	= _THIS_IP_;	p->softirqs_enabled		= 1;	p->softirq_context		= 0;#endif 	p->pagefault_disabled = 0; 	lockdep_init_task(p); 	p->blocked_on = NULL; /* not blocked yet */	p->blocked_donor = NULL; /* nobody is boosting p yet */ #ifdef CONFIG_BCACHE	p->sequential_io	= 0;	p->sequential_io_avg	= 0;#endif 	unwind_task_init(p); 	/* Perform scheduler related setup. Assign this task to a CPU. */	retval = sched_fork(clone_flags, p);	if (retval)		goto bad_fork_cleanup_policy; 	retval = perf_event_init_task(p, clone_flags);	if (retval)		goto bad_fork_sched_cancel_fork;	retval = audit_alloc(p);	if (retval)		goto bad_fork_cleanup_perf;	/* copy all the process information */	shm_init_task(p);	retval = security_task_alloc(p, clone_flags);	if (retval)		goto bad_fork_cleanup_audit;	retval = copy_semundo(clone_flags, p);	if (retval)		goto bad_fork_cleanup_security;	retval = copy_files(clone_flags, p, args->no_files);	if (retval)		goto bad_fork_cleanup_semundo;	retval = copy_fs(clone_flags, p);	if (retval)		goto bad_fork_cleanup_files;	retval = copy_sighand(clone_flags, p);	if (retval)		goto bad_fork_cleanup_fs;	retval = copy_signal(clone_flags, p);	if (retval)		goto bad_fork_cleanup_sighand;	retval = copy_mm(clone_flags, p);	if (retval)		goto bad_fork_cleanup_signal;	retval = copy_namespaces(clone_flags, p);	if (retval)		goto bad_fork_cleanup_mm;	retval = copy_io(clone_flags, p);	if (retval)		goto bad_fork_cleanup_namespaces;	retval = copy_thread(p, args);	if (retval)		goto bad_fork_cleanup_io; 	stackleak_task_init(p); 	if (pid != &init_struct_pid) {		pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid,				args->set_tid_size);		if (IS_ERR(pid)) {			retval = PTR_ERR(pid);			goto bad_fork_cleanup_thread;		}	} 	/*	 * This has to happen after we've potentially unshared the file	 * descriptor table (so that the pidfd doesn't leak into the child	 * if the fd table isn't shared).	 */	if (clone_flags & CLONE_PIDFD) {		unsigned flags = PIDFD_STALE; 		if (clone_flags & CLONE_THREAD)			flags |= PIDFD_THREAD;		if (clone_flags & CLONE_PIDFD_AUTOKILL)			flags |= PIDFD_AUTOKILL; 		/*		 * Note that no task has been attached to @pid yet indicate		 * that via CLONE_PIDFD.		 */		retval = pidfd_prepare(pid, flags, &pidfile);		if (retval < 0)			goto bad_fork_free_pid;		pidfd = retval; 		retval = put_user(pidfd, args->pidfd);		if (retval)			goto bad_fork_put_pidfd;	} #ifdef CONFIG_BLOCK	p->plug = NULL;	p->flags &= ~PF_BLOCK_TS;#endif	futex_init_task(p); 	/*	 * sigaltstack should be cleared when sharing the same VM	 */	if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)		sas_ss_reset(p); 	/*	 * Syscall tracing and stepping should be turned off in the	 * child regardless of CLONE_PTRACE.	 */	user_disable_single_step(p);	clear_task_syscall_work(p, SYSCALL_TRACE);#if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU)	clear_task_syscall_work(p, SYSCALL_EMU);#endif	clear_tsk_latency_tracing(p); 	/* ok, now we should be set up.. */	p->pid = pid_nr(pid);	if (clone_flags & CLONE_THREAD) {		p->group_leader = current->group_leader;		p->tgid = current->tgid;	} else {		p->group_leader = p;		p->tgid = p->pid;	} 	p->nr_dirtied = 0;	p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);	p->dirty_paused_when = 0; 	p->pdeath_signal = 0;	p->task_works = NULL;	clear_posix_cputimers_work(p); #ifdef CONFIG_KRETPROBES	p->kretprobe_instances.first = NULL;#endif#ifdef CONFIG_RETHOOK	p->rethooks.first = NULL;#endif 	/*	 * Ensure that the cgroup subsystem policies allow the new process to be	 * forked. It should be noted that the new process's css_set can be changed	 * between here and cgroup_post_fork() if an organisation operation is in	 * progress.	 */	retval = cgroup_can_fork(p, args);	if (retval)		goto bad_fork_put_pidfd; 	/*	 * Now that the cgroups are pinned, re-clone the parent cgroup and put	 * the new task on the correct runqueue. All this *before* the task	 * becomes visible.	 *	 * This isn't part of ->can_fork() because while the re-cloning is	 * cgroup specific, it unconditionally needs to place the task on a	 * runqueue.	 */	retval = sched_cgroup_fork(p, args);	if (retval)		goto bad_fork_cancel_cgroup; 	if (need_futex_hash_allocate_default(clone_flags)) {		retval = futex_hash_allocate_default();		if (retval)			goto bad_fork_cancel_cgroup;		/*		 * If we fail beyond this point we don't free the allocated		 * futex hash map. We assume that another thread will be created		 * and makes use of it. The hash map will be freed once the main		 * thread terminates.		 */	}	/*	 * From this point on we must avoid any synchronous user-space	 * communication until we take the tasklist-lock. In particular, we do	 * not want user-space to be able to predict the process start-time by	 * stalling fork(2) after we recorded the start_time but before it is	 * visible to the system.	 */ 	p->start_time = ktime_get_ns();	p->start_boottime = ktime_get_boottime_ns(); 	/*	 * Make it visible to the rest of the system, but dont wake it up yet.	 * Need tasklist lock for parent etc handling!	 */	write_lock_irq(&tasklist_lock); 	/* CLONE_PARENT re-uses the old parent */	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {		p->real_parent = current->real_parent;		p->parent_exec_id = current->parent_exec_id;		if (clone_flags & CLONE_THREAD)			p->exit_signal = -1;		else			p->exit_signal = current->group_leader->exit_signal;	} else {		p->real_parent = current;		p->parent_exec_id = current->self_exec_id;		p->exit_signal = args->exit_signal;	} 	klp_copy_process(p); 	sched_core_fork(p); 	spin_lock(&current->sighand->siglock); 	rv_task_fork(p); 	rseq_fork(p, clone_flags); 	/*	 * If zap_pid_ns_processes() was called after alloc_pid(), the new	 * child missed SIGKILL.  If current is not in the same namespace,	 * we can't rely on fatal_signal_pending() below.	 */	if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) {		retval = -ENOMEM;		goto bad_fork_core_free;	} 	/* Let kill terminate clone/fork in the middle */	if (fatal_signal_pending(current)) {		retval = -EINTR;		goto bad_fork_core_free;	} 	/* No more failure paths after this point. */ 	/*	 * Copy seccomp details explicitly here, in case they were changed	 * before holding sighand lock.	 */	copy_seccomp(p); 	if (clone_flags & CLONE_NNP)		task_set_no_new_privs(p); 	init_task_pid_links(p);	if (likely(p->pid)) {		ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 		init_task_pid(p, PIDTYPE_PID, pid);		if (thread_group_leader(p)) {			init_task_pid(p, PIDTYPE_TGID, pid);			init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));			init_task_pid(p, PIDTYPE_SID, task_session(current)); 			if (is_child_reaper(pid)) {

Lines 2001–2500 of 3409. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t