2240 lines · 52.0 KB · C
RawDownload
		rlim->rlim_cur = RLIM_INFINITY;	else		rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;	if (rlim64_is_infinity(rlim64->rlim_max))		rlim->rlim_max = RLIM_INFINITY;	else		rlim->rlim_max = (unsigned long)rlim64->rlim_max;} /* make sure you are allowed to change @tsk limits before calling this */int do_prlimit(struct task_struct *tsk, unsigned int resource,		struct rlimit *new_rlim, struct rlimit *old_rlim){	struct rlimit *rlim;	int retval = 0; 	if (resource >= RLIM_NLIMITS)		return -EINVAL;	if (new_rlim) {		if (new_rlim->rlim_cur > new_rlim->rlim_max)			return -EINVAL;		if (resource == RLIMIT_NOFILE &&				new_rlim->rlim_max > sysctl_nr_open)			return -EPERM;	} 	/* protect tsk->signal and tsk->sighand from disappearing */	read_lock(&tasklist_lock);	if (!tsk->sighand) {		retval = -ESRCH;		goto out;	} 	rlim = tsk->signal->rlim + resource;	task_lock(tsk->group_leader);	if (new_rlim) {		/* Keep the capable check against init_user_ns until		   cgroups can contain all limits */		if (new_rlim->rlim_max > rlim->rlim_max &&				!capable(CAP_SYS_RESOURCE))			retval = -EPERM;		if (!retval)			retval = security_task_setrlimit(tsk->group_leader,					resource, new_rlim);		if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {			/*			 * The caller is asking for an immediate RLIMIT_CPU			 * expiry.  But we use the zero value to mean "it was			 * never set".  So let's cheat and make it one second			 * instead			 */			new_rlim->rlim_cur = 1;		}	}	if (!retval) {		if (old_rlim)			*old_rlim = *rlim;		if (new_rlim)			*rlim = *new_rlim;	}	task_unlock(tsk->group_leader); 	/*	 * RLIMIT_CPU handling.   Note that the kernel fails to return an error	 * code if it rejected the user's attempt to set RLIMIT_CPU.  This is a	 * very long-standing error, and fixing it now risks breakage of	 * applications, so we live with it	 */	 if (!retval && new_rlim && resource == RLIMIT_CPU &&			 new_rlim->rlim_cur != RLIM_INFINITY)		update_rlimit_cpu(tsk, new_rlim->rlim_cur);out:	read_unlock(&tasklist_lock);	return retval;} /* rcu lock must be held */static int check_prlimit_permission(struct task_struct *task){	const struct cred *cred = current_cred(), *tcred; 	if (current == task)		return 0; 	tcred = __task_cred(task);	if (uid_eq(cred->uid, tcred->euid) &&	    uid_eq(cred->uid, tcred->suid) &&	    uid_eq(cred->uid, tcred->uid)  &&	    gid_eq(cred->gid, tcred->egid) &&	    gid_eq(cred->gid, tcred->sgid) &&	    gid_eq(cred->gid, tcred->gid))		return 0;	if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))		return 0; 	return -EPERM;} SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,		const struct rlimit64 __user *, new_rlim,		struct rlimit64 __user *, old_rlim){	struct rlimit64 old64, new64;	struct rlimit old, new;	struct task_struct *tsk;	int ret; 	if (new_rlim) {		if (copy_from_user(&new64, new_rlim, sizeof(new64)))			return -EFAULT;		rlim64_to_rlim(&new64, &new);	} 	rcu_read_lock();	tsk = pid ? find_task_by_vpid(pid) : current;	if (!tsk) {		rcu_read_unlock();		return -ESRCH;	}	ret = check_prlimit_permission(tsk);	if (ret) {		rcu_read_unlock();		return ret;	}	get_task_struct(tsk);	rcu_read_unlock(); 	ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,			old_rlim ? &old : NULL); 	if (!ret && old_rlim) {		rlim_to_rlim64(&old, &old64);		if (copy_to_user(old_rlim, &old64, sizeof(old64)))			ret = -EFAULT;	} 	put_task_struct(tsk);	return ret;} SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim){	struct rlimit new_rlim; 	if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))		return -EFAULT;	return do_prlimit(current, resource, &new_rlim, NULL);} /* * It would make sense to put struct rusage in the task_struct, * except that would make the task_struct be *really big*.  After * task_struct gets moved into malloc'ed memory, it would * make sense to do this.  It will make moving the rest of the information * a lot simpler!  (Which we're not doing right now because we're not * measuring them yet). * * When sampling multiple threads for RUSAGE_SELF, under SMP we might have * races with threads incrementing their own counters.  But since word * reads are atomic, we either get new values or old values and we don't * care which for the sums.  We always take the siglock to protect reading * the c* fields from p->signal from races with exit.c updating those * fields when reaping, so a sample either gets all the additions of a * given child after it's reaped, or none so this sample is before reaping. * * Locking: * We need to take the siglock for CHILDEREN, SELF and BOTH * for  the cases current multithreaded, non-current single threaded * non-current multithreaded.  Thread traversal is now safe with * the siglock held. * Strictly speaking, we donot need to take the siglock if we are current and * single threaded,  as no one else can take our signal_struct away, no one * else can  reap the  children to update signal->c* counters, and no one else * can race with the signal-> fields. If we do not take any lock, the * signal-> fields could be read out of order while another thread was just * exiting. So we should  place a read memory barrier when we avoid the lock. * On the writer side,  write memory barrier is implied in  __exit_signal * as __exit_signal releases  the siglock spinlock after updating the signal-> * fields. But we don't do this yet to keep things simple. * */ static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r){	r->ru_nvcsw += t->nvcsw;	r->ru_nivcsw += t->nivcsw;	r->ru_minflt += t->min_flt;	r->ru_majflt += t->maj_flt;	r->ru_inblock += task_io_get_inblock(t);	r->ru_oublock += task_io_get_oublock(t);} static void k_getrusage(struct task_struct *p, int who, struct rusage *r){	struct task_struct *t;	unsigned long flags;	cputime_t tgutime, tgstime, utime, stime;	unsigned long maxrss = 0; 	memset((char *) r, 0, sizeof *r);	utime = stime = 0; 	if (who == RUSAGE_THREAD) {		task_times(current, &utime, &stime);		accumulate_thread_rusage(p, r);		maxrss = p->signal->maxrss;		goto out;	} 	if (!lock_task_sighand(p, &flags))		return; 	switch (who) {		case RUSAGE_BOTH:		case RUSAGE_CHILDREN:			utime = p->signal->cutime;			stime = p->signal->cstime;			r->ru_nvcsw = p->signal->cnvcsw;			r->ru_nivcsw = p->signal->cnivcsw;			r->ru_minflt = p->signal->cmin_flt;			r->ru_majflt = p->signal->cmaj_flt;			r->ru_inblock = p->signal->cinblock;			r->ru_oublock = p->signal->coublock;			maxrss = p->signal->cmaxrss; 			if (who == RUSAGE_CHILDREN)				break; 		case RUSAGE_SELF:			thread_group_times(p, &tgutime, &tgstime);			utime += tgutime;			stime += tgstime;			r->ru_nvcsw += p->signal->nvcsw;			r->ru_nivcsw += p->signal->nivcsw;			r->ru_minflt += p->signal->min_flt;			r->ru_majflt += p->signal->maj_flt;			r->ru_inblock += p->signal->inblock;			r->ru_oublock += p->signal->oublock;			if (maxrss < p->signal->maxrss)				maxrss = p->signal->maxrss;			t = p;			do {				accumulate_thread_rusage(t, r);				t = next_thread(t);			} while (t != p);			break; 		default:			BUG();	}	unlock_task_sighand(p, &flags); out:	cputime_to_timeval(utime, &r->ru_utime);	cputime_to_timeval(stime, &r->ru_stime); 	if (who != RUSAGE_CHILDREN) {		struct mm_struct *mm = get_task_mm(p);		if (mm) {			setmax_mm_hiwater_rss(&maxrss, mm);			mmput(mm);		}	}	r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */} int getrusage(struct task_struct *p, int who, struct rusage __user *ru){	struct rusage r;	k_getrusage(p, who, &r);	return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;} SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru){	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&	    who != RUSAGE_THREAD)		return -EINVAL;	return getrusage(current, who, ru);} SYSCALL_DEFINE1(umask, int, mask){	mask = xchg(&current->fs->umask, mask & S_IRWXUGO);	return mask;} #ifdef CONFIG_CHECKPOINT_RESTOREstatic int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd){	struct vm_area_struct *vma;	struct file *exe_file;	struct dentry *dentry;	int err; 	exe_file = fget(fd);	if (!exe_file)		return -EBADF; 	dentry = exe_file->f_path.dentry; 	/*	 * Because the original mm->exe_file points to executable file, make	 * sure that this one is executable as well, to avoid breaking an	 * overall picture.	 */	err = -EACCES;	if (!S_ISREG(dentry->d_inode->i_mode)	||	    exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)		goto exit; 	err = inode_permission(dentry->d_inode, MAY_EXEC);	if (err)		goto exit; 	down_write(&mm->mmap_sem); 	/*	 * Forbid mm->exe_file change if there are mapped other files.	 */	err = -EBUSY;	for (vma = mm->mmap; vma; vma = vma->vm_next) {		if (vma->vm_file && !path_equal(&vma->vm_file->f_path,						&exe_file->f_path))			goto exit_unlock;	} 	/*	 * The symlink can be changed only once, just to disallow arbitrary	 * transitions malicious software might bring in. This means one	 * could make a snapshot over all processes running and monitor	 * /proc/pid/exe changes to notice unusual activity if needed.	 */	err = -EPERM;	if (test_and_set_bit(MMF_EXE_FILE_CHANGED, &mm->flags))		goto exit_unlock; 	set_mm_exe_file(mm, exe_file);exit_unlock:	up_write(&mm->mmap_sem); exit:	fput(exe_file);	return err;} static int prctl_set_mm(int opt, unsigned long addr,			unsigned long arg4, unsigned long arg5){	unsigned long rlim = rlimit(RLIMIT_DATA);	struct mm_struct *mm = current->mm;	struct vm_area_struct *vma;	int error; 	if (arg5 || (arg4 && opt != PR_SET_MM_AUXV))		return -EINVAL; 	if (!capable(CAP_SYS_RESOURCE))		return -EPERM; 	if (opt == PR_SET_MM_EXE_FILE)		return prctl_set_mm_exe_file(mm, (unsigned int)addr); 	if (addr >= TASK_SIZE || addr < mmap_min_addr)		return -EINVAL; 	error = -EINVAL; 	down_read(&mm->mmap_sem);	vma = find_vma(mm, addr); 	switch (opt) {	case PR_SET_MM_START_CODE:		mm->start_code = addr;		break;	case PR_SET_MM_END_CODE:		mm->end_code = addr;		break;	case PR_SET_MM_START_DATA:		mm->start_data = addr;		break;	case PR_SET_MM_END_DATA:		mm->end_data = addr;		break; 	case PR_SET_MM_START_BRK:		if (addr <= mm->end_data)			goto out; 		if (rlim < RLIM_INFINITY &&		    (mm->brk - addr) +		    (mm->end_data - mm->start_data) > rlim)			goto out; 		mm->start_brk = addr;		break; 	case PR_SET_MM_BRK:		if (addr <= mm->end_data)			goto out; 		if (rlim < RLIM_INFINITY &&		    (addr - mm->start_brk) +		    (mm->end_data - mm->start_data) > rlim)			goto out; 		mm->brk = addr;		break; 	/*	 * If command line arguments and environment	 * are placed somewhere else on stack, we can	 * set them up here, ARG_START/END to setup	 * command line argumets and ENV_START/END	 * for environment.	 */	case PR_SET_MM_START_STACK:	case PR_SET_MM_ARG_START:	case PR_SET_MM_ARG_END:	case PR_SET_MM_ENV_START:	case PR_SET_MM_ENV_END:		if (!vma) {			error = -EFAULT;			goto out;		}		if (opt == PR_SET_MM_START_STACK)			mm->start_stack = addr;		else if (opt == PR_SET_MM_ARG_START)			mm->arg_start = addr;		else if (opt == PR_SET_MM_ARG_END)			mm->arg_end = addr;		else if (opt == PR_SET_MM_ENV_START)			mm->env_start = addr;		else if (opt == PR_SET_MM_ENV_END)			mm->env_end = addr;		break; 	/*	 * This doesn't move auxiliary vector itself	 * since it's pinned to mm_struct, but allow	 * to fill vector with new values. It's up	 * to a caller to provide sane values here	 * otherwise user space tools which use this	 * vector might be unhappy.	 */	case PR_SET_MM_AUXV: {		unsigned long user_auxv[AT_VECTOR_SIZE]; 		if (arg4 > sizeof(user_auxv))			goto out;		up_read(&mm->mmap_sem); 		if (copy_from_user(user_auxv, (const void __user *)addr, arg4))			return -EFAULT; 		/* Make sure the last entry is always AT_NULL */		user_auxv[AT_VECTOR_SIZE - 2] = 0;		user_auxv[AT_VECTOR_SIZE - 1] = 0; 		BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv)); 		task_lock(current);		memcpy(mm->saved_auxv, user_auxv, arg4);		task_unlock(current); 		return 0;	}	default:		goto out;	} 	error = 0;out:	up_read(&mm->mmap_sem);	return error;} static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr){	return put_user(me->clear_child_tid, tid_addr);} #else /* CONFIG_CHECKPOINT_RESTORE */static int prctl_set_mm(int opt, unsigned long addr,			unsigned long arg4, unsigned long arg5){	return -EINVAL;}static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr){	return -EINVAL;}#endif SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,		unsigned long, arg4, unsigned long, arg5){	struct task_struct *me = current;	unsigned char comm[sizeof(me->comm)];	long error;

Lines 1501–2000 of 2240. 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