base.c 83 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  linux/fs/proc/base.c
 *
 *  Copyright (C) 1991, 1992 Linus Torvalds
 *
 *  proc base directory handling functions
 *
 *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
 *  Instead of using magical inumbers to determine the kind of object
 *  we allocate and fill in-core inodes upon lookup. They don't even
 *  go into icache. We cache the reference to task_struct upon lookup too.
 *  Eventually it should become a filesystem in its own. We don't use the
 *  rest of procfs anymore.
Mauricio Lin's avatar
Mauricio Lin committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 *
 *
 *  Changelog:
 *  17-Jan-2005
 *  Allan Bezerra
 *  Bruna Moreira <bruna.moreira@indt.org.br>
 *  Edjard Mota <edjard.mota@indt.org.br>
 *  Ilias Biris <ilias.biris@indt.org.br>
 *  Mauricio Lin <mauricio.lin@indt.org.br>
 *
 *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
 *
 *  A new process specific entry (smaps) included in /proc. It shows the
 *  size of rss for each memory area. The maps entry lacks information
 *  about physical memory size (rss) for each mapped file, i.e.,
 *  rss information for executables and library files.
 *  This additional information is useful for any tools that need to know
 *  about physical memory consumption for a process specific library.
 *
 *  Changelog:
 *  21-Feb-2005
 *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
 *  Pud inclusion in the page table walking.
 *
 *  ChangeLog:
 *  10-Mar-2005
 *  10LE Instituto Nokia de Tecnologia - INdT:
 *  A better way to walks through the page table as suggested by Hugh Dickins.
 *
 *  Simo Piiroinen <simo.piiroinen@nokia.com>:
 *  Smaps information related to shared, private, clean and dirty pages.
 *
 *  Paul Mundt <paul.mundt@nokia.com>:
 *  Overall revision about smaps.
Linus Torvalds's avatar
Linus Torvalds committed
48 49 50 51 52 53 54 55
 */

#include <asm/uaccess.h>

#include <linux/errno.h>
#include <linux/time.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
56
#include <linux/task_io_accounting_ops.h>
Linus Torvalds's avatar
Linus Torvalds committed
57
#include <linux/init.h>
58
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
59
#include <linux/file.h>
Al Viro's avatar
Al Viro committed
60
#include <linux/fdtable.h>
Linus Torvalds's avatar
Linus Torvalds committed
61 62 63
#include <linux/string.h>
#include <linux/seq_file.h>
#include <linux/namei.h>
64
#include <linux/mnt_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
65
#include <linux/mm.h>
66
#include <linux/swap.h>
67
#include <linux/rcupdate.h>
Linus Torvalds's avatar
Linus Torvalds committed
68
#include <linux/kallsyms.h>
Ken Chen's avatar
Ken Chen committed
69
#include <linux/stacktrace.h>
70
#include <linux/resource.h>
Kees Cook's avatar
Kees Cook committed
71
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
72 73 74
#include <linux/mount.h>
#include <linux/security.h>
#include <linux/ptrace.h>
75
#include <linux/tracehook.h>
Andrew Morton's avatar
Andrew Morton committed
76
#include <linux/printk.h>
77
#include <linux/cgroup.h>
Linus Torvalds's avatar
Linus Torvalds committed
78 79
#include <linux/cpuset.h>
#include <linux/audit.h>
Al Viro's avatar
Al Viro committed
80
#include <linux/poll.h>
81
#include <linux/nsproxy.h>
82
#include <linux/oom.h>
83
#include <linux/elf.h>
84
#include <linux/pid_namespace.h>
85
#include <linux/user_namespace.h>
86
#include <linux/fs_struct.h>
87
#include <linux/slab.h>
88
#include <linux/flex_array.h>
89
#include <linux/posix-timers.h>
90 91 92
#ifdef CONFIG_HARDWALL
#include <asm/hardwall.h>
#endif
93
#include <trace/events/oom.h>
Linus Torvalds's avatar
Linus Torvalds committed
94
#include "internal.h"
95
#include "fd.h"
Linus Torvalds's avatar
Linus Torvalds committed
96

97 98 99 100 101 102 103 104 105 106
/* NOTE:
 *	Implementing inode permission operations in /proc is almost
 *	certainly an error.  Permission checks need to happen during
 *	each system call not at open time.  The reason is that most of
 *	what we wish to check for permissions in /proc varies at runtime.
 *
 *	The classic example of a problem is opening file descriptors
 *	in /proc for a task before it execs a suid executable.
 */

Linus Torvalds's avatar
Linus Torvalds committed
107
struct pid_entry {
108
	const char *name;
109
	int len;
Al Viro's avatar
Al Viro committed
110
	umode_t mode;
111
	const struct inode_operations *iop;
112
	const struct file_operations *fop;
113
	union proc_op op;
Linus Torvalds's avatar
Linus Torvalds committed
114 115
};

116
#define NOD(NAME, MODE, IOP, FOP, OP) {			\
117
	.name = (NAME),					\
118
	.len  = sizeof(NAME) - 1,			\
119 120 121 122 123 124
	.mode = MODE,					\
	.iop  = IOP,					\
	.fop  = FOP,					\
	.op   = OP,					\
}

Alexey Dobriyan's avatar
Alexey Dobriyan committed
125 126 127
#define DIR(NAME, MODE, iops, fops)	\
	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
#define LNK(NAME, get_link)					\
128
	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
129
		&proc_pid_link_inode_operations, NULL,		\
Alexey Dobriyan's avatar
Alexey Dobriyan committed
130 131 132 133
		{ .proc_get_link = get_link } )
#define REG(NAME, MODE, fops)				\
	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
#define ONE(NAME, MODE, show)				\
134 135
	NOD(NAME, (S_IFREG|(MODE)), 			\
		NULL, &proc_single_file_operations,	\
Alexey Dobriyan's avatar
Alexey Dobriyan committed
136
		{ .proc_show = show } )
Linus Torvalds's avatar
Linus Torvalds committed
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/*
 * Count the number of hardlinks for the pid_entry table, excluding the .
 * and .. links.
 */
static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
	unsigned int n)
{
	unsigned int i;
	unsigned int count;

	count = 0;
	for (i = 0; i < n; ++i) {
		if (S_ISDIR(entries[i].mode))
			++count;
	}

	return count;
}

157
static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds's avatar
Linus Torvalds committed
158
{
Hugh Dickins's avatar
Hugh Dickins committed
159 160
	int result = -ENOENT;

161
	task_lock(task);
162 163
	if (task->fs) {
		get_fs_root(task->fs, root);
Hugh Dickins's avatar
Hugh Dickins committed
164 165
		result = 0;
	}
166
	task_unlock(task);
Hugh Dickins's avatar
Hugh Dickins committed
167
	return result;
168 169
}

170
static int proc_cwd_link(struct dentry *dentry, struct path *path)
171
{
172
	struct task_struct *task = get_proc_task(d_inode(dentry));
173
	int result = -ENOENT;
174 175

	if (task) {
176 177 178 179 180 181
		task_lock(task);
		if (task->fs) {
			get_fs_pwd(task->fs, path);
			result = 0;
		}
		task_unlock(task);
182 183
		put_task_struct(task);
	}
Linus Torvalds's avatar
Linus Torvalds committed
184 185 186
	return result;
}

187
static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds's avatar
Linus Torvalds committed
188
{
189
	struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds's avatar
Linus Torvalds committed
190
	int result = -ENOENT;
191 192

	if (task) {
193
		result = get_task_root(task, path);
194 195
		put_task_struct(task);
	}
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198
	return result;
}

199 200
static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
				     size_t _count, loff_t *pos)
Linus Torvalds's avatar
Linus Torvalds committed
201
{
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	struct task_struct *tsk;
	struct mm_struct *mm;
	char *page;
	unsigned long count = _count;
	unsigned long arg_start, arg_end, env_start, env_end;
	unsigned long len1, len2, len;
	unsigned long p;
	char c;
	ssize_t rv;

	BUG_ON(*pos < 0);

	tsk = get_proc_task(file_inode(file));
	if (!tsk)
		return -ESRCH;
	mm = get_task_mm(tsk);
	put_task_struct(tsk);
	if (!mm)
		return 0;
	/* Check if process spawned far enough to have cmdline. */
	if (!mm->env_end) {
		rv = 0;
		goto out_mmput;
	}

	page = (char *)__get_free_page(GFP_TEMPORARY);
	if (!page) {
		rv = -ENOMEM;
		goto out_mmput;
	}

	down_read(&mm->mmap_sem);
	arg_start = mm->arg_start;
	arg_end = mm->arg_end;
	env_start = mm->env_start;
	env_end = mm->env_end;
	up_read(&mm->mmap_sem);

	BUG_ON(arg_start > arg_end);
	BUG_ON(env_start > env_end);

	len1 = arg_end - arg_start;
	len2 = env_end - env_start;

246 247 248 249 250
	/* Empty ARGV. */
	if (len1 == 0) {
		rv = 0;
		goto out_free_page;
	}
251
	/*
252 253
	 * Inherently racy -- command line shares address space
	 * with code and data.
254
	 */
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
	if (rv <= 0)
		goto out_free_page;

	rv = 0;

	if (c == '\0') {
		/* Command line (set of strings) occupies whole ARGV. */
		if (len1 <= *pos)
			goto out_free_page;

		p = arg_start + *pos;
		len = len1 - *pos;
		while (count > 0 && len > 0) {
			unsigned int _count;
			int nr_read;

			_count = min3(count, len, PAGE_SIZE);
			nr_read = access_remote_vm(mm, p, page, _count, 0);
			if (nr_read < 0)
				rv = nr_read;
			if (nr_read <= 0)
				goto out_free_page;

			if (copy_to_user(buf, page, nr_read)) {
				rv = -EFAULT;
				goto out_free_page;
			}

			p	+= nr_read;
			len	-= nr_read;
			buf	+= nr_read;
			count	-= nr_read;
			rv	+= nr_read;
		}
	} else {
		/*
		 * Command line (1 string) occupies ARGV and maybe
		 * extends into ENVP.
		 */
		if (len1 + len2 <= *pos)
			goto skip_argv_envp;
		if (len1 <= *pos)
			goto skip_argv;

		p = arg_start + *pos;
		len = len1 - *pos;
		while (count > 0 && len > 0) {
			unsigned int _count, l;
			int nr_read;
			bool final;

			_count = min3(count, len, PAGE_SIZE);
			nr_read = access_remote_vm(mm, p, page, _count, 0);
			if (nr_read < 0)
				rv = nr_read;
			if (nr_read <= 0)
				goto out_free_page;

			/*
			 * Command line can be shorter than whole ARGV
			 * even if last "marker" byte says it is not.
			 */
			final = false;
			l = strnlen(page, nr_read);
			if (l < nr_read) {
				nr_read = l;
				final = true;
			}

			if (copy_to_user(buf, page, nr_read)) {
				rv = -EFAULT;
				goto out_free_page;
			}

			p	+= nr_read;
			len	-= nr_read;
			buf	+= nr_read;
			count	-= nr_read;
			rv	+= nr_read;

			if (final)
				goto out_free_page;
		}
skip_argv:
		/*
		 * Command line (1 string) occupies ARGV and
		 * extends into ENVP.
		 */
		if (len1 <= *pos) {
			p = env_start + *pos - len1;
			len = len1 + len2 - *pos;
		} else {
			p = env_start;
			len = len2;
		}
		while (count > 0 && len > 0) {
			unsigned int _count, l;
			int nr_read;
			bool final;

			_count = min3(count, len, PAGE_SIZE);
			nr_read = access_remote_vm(mm, p, page, _count, 0);
			if (nr_read < 0)
				rv = nr_read;
			if (nr_read <= 0)
				goto out_free_page;

			/* Find EOS. */
			final = false;
			l = strnlen(page, nr_read);
			if (l < nr_read) {
				nr_read = l;
				final = true;
			}

			if (copy_to_user(buf, page, nr_read)) {
				rv = -EFAULT;
				goto out_free_page;
			}

			p	+= nr_read;
			len	-= nr_read;
			buf	+= nr_read;
			count	-= nr_read;
			rv	+= nr_read;

			if (final)
				goto out_free_page;
		}
skip_argv_envp:
		;
	}

out_free_page:
	free_page((unsigned long)page);
out_mmput:
	mmput(mm);
	if (rv > 0)
		*pos += rv;
	return rv;
Linus Torvalds's avatar
Linus Torvalds committed
396 397
}

398 399 400 401 402
static const struct file_operations proc_pid_cmdline_ops = {
	.read	= proc_pid_cmdline_read,
	.llseek	= generic_file_llseek,
};

403 404
static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
			 struct pid *pid, struct task_struct *task)
Linus Torvalds's avatar
Linus Torvalds committed
405
{
406
	struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
407
	if (mm && !IS_ERR(mm)) {
Linus Torvalds's avatar
Linus Torvalds committed
408
		unsigned int nwords = 0;
Hannes Eder's avatar
Hannes Eder committed
409
		do {
Linus Torvalds's avatar
Linus Torvalds committed
410
			nwords += 2;
Hannes Eder's avatar
Hannes Eder committed
411
		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
412
		seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
Linus Torvalds's avatar
Linus Torvalds committed
413
		mmput(mm);
414 415 416
		return 0;
	} else
		return PTR_ERR(mm);
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419 420 421 422 423 424
}


#ifdef CONFIG_KALLSYMS
/*
 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
 * Returns the resolved symbol.  If that fails, simply return the address.
 */
425 426
static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
			  struct pid *pid, struct task_struct *task)
Linus Torvalds's avatar
Linus Torvalds committed
427
{
Alexey Dobriyan's avatar
Alexey Dobriyan committed
428
	unsigned long wchan;
429
	char symname[KSYM_NAME_LEN];
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432

	wchan = get_wchan(task);

433 434
	if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
			&& !lookup_symbol_name(wchan, symname))
435
		seq_printf(m, "%s", symname);
436
	else
437
		seq_putc(m, '0');
438 439

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
440 441 442
}
#endif /* CONFIG_KALLSYMS */

443 444 445 446 447
static int lock_trace(struct task_struct *task)
{
	int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
	if (err)
		return err;
448
	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
449 450 451 452 453 454 455 456 457 458 459
		mutex_unlock(&task->signal->cred_guard_mutex);
		return -EPERM;
	}
	return 0;
}

static void unlock_trace(struct task_struct *task)
{
	mutex_unlock(&task->signal->cred_guard_mutex);
}

Ken Chen's avatar
Ken Chen committed
460 461 462 463 464 465 466 467 468
#ifdef CONFIG_STACKTRACE

#define MAX_STACK_TRACE_DEPTH	64

static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
			  struct pid *pid, struct task_struct *task)
{
	struct stack_trace trace;
	unsigned long *entries;
469
	int err;
Ken Chen's avatar
Ken Chen committed
470 471 472 473 474 475 476 477 478 479 480
	int i;

	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
	if (!entries)
		return -ENOMEM;

	trace.nr_entries	= 0;
	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
	trace.entries		= entries;
	trace.skip		= 0;

481 482 483 484 485
	err = lock_trace(task);
	if (!err) {
		save_stack_trace_tsk(task, &trace);

		for (i = 0; i < trace.nr_entries; i++) {
486
			seq_printf(m, "[<%pK>] %pS\n",
487 488 489
				   (void *)entries[i], (void *)entries[i]);
		}
		unlock_trace(task);
Ken Chen's avatar
Ken Chen committed
490 491 492
	}
	kfree(entries);

493
	return err;
Ken Chen's avatar
Ken Chen committed
494 495 496
}
#endif

497
#ifdef CONFIG_SCHED_INFO
Linus Torvalds's avatar
Linus Torvalds committed
498 499 500
/*
 * Provides /proc/PID/schedstat
 */
501 502
static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
			      struct pid *pid, struct task_struct *task)
Linus Torvalds's avatar
Linus Torvalds committed
503
{
504 505 506 507
	if (unlikely(!sched_info_on()))
		seq_printf(m, "0 0 0\n");
	else
		seq_printf(m, "%llu %llu %lu\n",
508 509 510 511 512
		   (unsigned long long)task->se.sum_exec_runtime,
		   (unsigned long long)task->sched_info.run_delay,
		   task->sched_info.pcount);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
513 514 515
}
#endif

Arjan van de Ven's avatar
Arjan van de Ven committed
516 517 518 519
#ifdef CONFIG_LATENCYTOP
static int lstats_show_proc(struct seq_file *m, void *v)
{
	int i;
520 521
	struct inode *inode = m->private;
	struct task_struct *task = get_proc_task(inode);
Arjan van de Ven's avatar
Arjan van de Ven committed
522

523 524 525
	if (!task)
		return -ESRCH;
	seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven's avatar
Arjan van de Ven committed
526
	for (i = 0; i < 32; i++) {
527 528
		struct latency_record *lr = &task->latency_record[i];
		if (lr->backtrace[0]) {
Arjan van de Ven's avatar
Arjan van de Ven committed
529
			int q;
530 531
			seq_printf(m, "%i %li %li",
				   lr->count, lr->time, lr->max);
Arjan van de Ven's avatar
Arjan van de Ven committed
532
			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
533 534
				unsigned long bt = lr->backtrace[q];
				if (!bt)
Arjan van de Ven's avatar
Arjan van de Ven committed
535
					break;
536
				if (bt == ULONG_MAX)
Arjan van de Ven's avatar
Arjan van de Ven committed
537
					break;
538
				seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven's avatar
Arjan van de Ven committed
539
			}
540
			seq_putc(m, '\n');
Arjan van de Ven's avatar
Arjan van de Ven committed
541 542 543
		}

	}
544
	put_task_struct(task);
Arjan van de Ven's avatar
Arjan van de Ven committed
545 546 547 548 549
	return 0;
}

static int lstats_open(struct inode *inode, struct file *file)
{
550
	return single_open(file, lstats_show_proc, inode);
551 552
}

Arjan van de Ven's avatar
Arjan van de Ven committed
553 554 555
static ssize_t lstats_write(struct file *file, const char __user *buf,
			    size_t count, loff_t *offs)
{
Al Viro's avatar
Al Viro committed
556
	struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven's avatar
Arjan van de Ven committed
557

558 559
	if (!task)
		return -ESRCH;
Arjan van de Ven's avatar
Arjan van de Ven committed
560
	clear_all_latency_tracing(task);
561
	put_task_struct(task);
Arjan van de Ven's avatar
Arjan van de Ven committed
562 563 564 565 566 567 568 569 570

	return count;
}

static const struct file_operations proc_lstats_operations = {
	.open		= lstats_open,
	.read		= seq_read,
	.write		= lstats_write,
	.llseek		= seq_lseek,
571
	.release	= single_release,
Arjan van de Ven's avatar
Arjan van de Ven committed
572 573 574 575
};

#endif

576 577
static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
			  struct pid *pid, struct task_struct *task)
Linus Torvalds's avatar
Linus Torvalds committed
578
{
579
	unsigned long totalpages = totalram_pages + total_swap_pages;
580
	unsigned long points = 0;
Linus Torvalds's avatar
Linus Torvalds committed
581

582 583
	points = oom_badness(task, NULL, NULL, totalpages) *
					1000 / totalpages;
584 585 586
	seq_printf(m, "%lu\n", points);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
587 588
}

589
struct limit_names {
590 591
	const char *name;
	const char *unit;
592 593 594
};

static const struct limit_names lnames[RLIM_NLIMITS] = {
595
	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
596 597 598 599 600 601 602 603 604 605 606 607 608 609
	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
	[RLIMIT_DATA] = {"Max data size", "bytes"},
	[RLIMIT_STACK] = {"Max stack size", "bytes"},
	[RLIMIT_CORE] = {"Max core file size", "bytes"},
	[RLIMIT_RSS] = {"Max resident set", "bytes"},
	[RLIMIT_NPROC] = {"Max processes", "processes"},
	[RLIMIT_NOFILE] = {"Max open files", "files"},
	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
	[RLIMIT_AS] = {"Max address space", "bytes"},
	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
	[RLIMIT_NICE] = {"Max nice priority", NULL},
	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
610
	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
611 612 613
};

/* Display limits for a process */
614 615
static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
			   struct pid *pid, struct task_struct *task)
616 617 618 619 620 621
{
	unsigned int i;
	unsigned long flags;

	struct rlimit rlim[RLIM_NLIMITS];

622
	if (!lock_task_sighand(task, &flags))
623 624 625 626 627 628 629
		return 0;
	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
	unlock_task_sighand(task, &flags);

	/*
	 * print the file header
	 */
630
       seq_printf(m, "%-25s %-20s %-20s %-10s\n",
631
		  "Limit", "Soft Limit", "Hard Limit", "Units");
632 633 634

	for (i = 0; i < RLIM_NLIMITS; i++) {
		if (rlim[i].rlim_cur == RLIM_INFINITY)
635
			seq_printf(m, "%-25s %-20s ",
636
				   lnames[i].name, "unlimited");
637
		else
638
			seq_printf(m, "%-25s %-20lu ",
639
				   lnames[i].name, rlim[i].rlim_cur);
640 641

		if (rlim[i].rlim_max == RLIM_INFINITY)
642
			seq_printf(m, "%-20s ", "unlimited");
643
		else
644
			seq_printf(m, "%-20lu ", rlim[i].rlim_max);
645 646

		if (lnames[i].unit)
647
			seq_printf(m, "%-10s\n", lnames[i].unit);
648
		else
649
			seq_putc(m, '\n');
650 651
	}

652
	return 0;
653 654
}

Roland McGrath's avatar
Roland McGrath committed
655
#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
656 657
static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
			    struct pid *pid, struct task_struct *task)
Roland McGrath's avatar
Roland McGrath committed
658 659 660
{
	long nr;
	unsigned long args[6], sp, pc;
661 662 663
	int res;

	res = lock_trace(task);
664 665
	if (res)
		return res;
Roland McGrath's avatar
Roland McGrath committed
666 667

	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
668
		seq_puts(m, "running\n");
669
	else if (nr < 0)
670
		seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
671
	else
672
		seq_printf(m,
Roland McGrath's avatar
Roland McGrath committed
673 674 675 676
		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
		       nr,
		       args[0], args[1], args[2], args[3], args[4], args[5],
		       sp, pc);
677
	unlock_trace(task);
678 679

	return 0;
Roland McGrath's avatar
Roland McGrath committed
680 681 682
}
#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */

Linus Torvalds's avatar
Linus Torvalds committed
683 684 685 686 687
/************************************************************************/
/*                       Here the fs part begins                        */
/************************************************************************/

/* permission checks */
688
static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
689
{
690 691
	struct task_struct *task;
	int allowed = 0;
692 693 694
	/* Allow access to a task's file descriptors if it is us or we
	 * may use ptrace attach to the process and find out that
	 * information.
695 696
	 */
	task = get_proc_task(inode);
697
	if (task) {
698
		allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
699
		put_task_struct(task);
700
	}
701
	return allowed;
Linus Torvalds's avatar
Linus Torvalds committed
702 703
}

704
int proc_setattr(struct dentry *dentry, struct iattr *attr)
705 706
{
	int error;
707
	struct inode *inode = d_inode(dentry);
708 709 710 711

	if (attr->ia_valid & ATTR_MODE)
		return -EPERM;

712
	error = setattr_prepare(dentry, attr);
Christoph Hellwig's avatar
Christoph Hellwig committed
713 714 715 716 717 718
	if (error)
		return error;

	setattr_copy(inode, attr);
	mark_inode_dirty(inode);
	return 0;
719 720
}

721 722 723 724 725 726 727 728 729 730 731 732
/*
 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
 * or euid/egid (for hide_pid_min=2)?
 */
static bool has_pid_permissions(struct pid_namespace *pid,
				 struct task_struct *task,
				 int hide_pid_min)
{
	if (pid->hide_pid < hide_pid_min)
		return true;
	if (in_group_p(pid->pid_gid))
		return true;
733
	return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
734 735 736 737 738 739 740 741 742 743
}


static int proc_pid_permission(struct inode *inode, int mask)
{
	struct pid_namespace *pid = inode->i_sb->s_fs_info;
	struct task_struct *task;
	bool has_perms;

	task = get_proc_task(inode);
744 745
	if (!task)
		return -ESRCH;
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	has_perms = has_pid_permissions(pid, task, 1);
	put_task_struct(task);

	if (!has_perms) {
		if (pid->hide_pid == 2) {
			/*
			 * Let's make getdents(), stat(), and open()
			 * consistent with each other.  If a process
			 * may not stat() a file, it shouldn't be seen
			 * in procfs at all.
			 */
			return -ENOENT;
		}

		return -EPERM;
	}
	return generic_permission(inode, mask);
}



767
static const struct inode_operations proc_def_inode_operations = {
768 769 770
	.setattr	= proc_setattr,
};

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
static int proc_single_show(struct seq_file *m, void *v)
{
	struct inode *inode = m->private;
	struct pid_namespace *ns;
	struct pid *pid;
	struct task_struct *task;
	int ret;

	ns = inode->i_sb->s_fs_info;
	pid = proc_pid(inode);
	task = get_pid_task(pid, PIDTYPE_PID);
	if (!task)
		return -ESRCH;

	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);

	put_task_struct(task);
	return ret;
}

static int proc_single_open(struct inode *inode, struct file *filp)
{
793
	return single_open(filp, proc_single_show, inode);
794 795 796 797 798 799 800 801 802
}

static const struct file_operations proc_single_file_operations = {
	.open		= proc_single_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

803 804

struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
Linus Torvalds's avatar
Linus Torvalds committed
805
{
806 807
	struct task_struct *task = get_proc_task(inode);
	struct mm_struct *mm = ERR_PTR(-ESRCH);
808

809
	if (task) {
810
		mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
811
		put_task_struct(task);
812

813 814 815 816 817 818 819 820 821 822 823 824 825 826
		if (!IS_ERR_OR_NULL(mm)) {
			/* ensure this mm_struct can't be freed */
			atomic_inc(&mm->mm_count);
			/* but do not pin its memory */
			mmput(mm);
		}
	}

	return mm;
}

static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
{
	struct mm_struct *mm = proc_mem_open(inode, mode);
827 828 829 830 831

	if (IS_ERR(mm))
		return PTR_ERR(mm);

	file->private_data = mm;
Linus Torvalds's avatar
Linus Torvalds committed
832 833 834
	return 0;
}

835 836
static int mem_open(struct inode *inode, struct file *file)
{
837 838 839 840 841 842
	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);

	/* OK to pass negative loff_t, we can catch out-of-range */
	file->f_mode |= FMODE_UNSIGNED_OFFSET;

	return ret;
843 844
}

845 846
static ssize_t mem_rw(struct file *file, char __user *buf,
			size_t count, loff_t *ppos, int write)
Linus Torvalds's avatar
Linus Torvalds committed
847
{
848
	struct mm_struct *mm = file->private_data;
849 850
	unsigned long addr = *ppos;
	ssize_t copied;
Linus Torvalds's avatar
Linus Torvalds committed
851 852
	char *page;

853 854
	if (!mm)
		return 0;
855

856 857
	page = (char *)__get_free_page(GFP_TEMPORARY);
	if (!page)
858
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
859

860
	copied = 0;
861 862 863
	if (!atomic_inc_not_zero(&mm->mm_users))
		goto free;

Linus Torvalds's avatar
Linus Torvalds committed
864
	while (count > 0) {
865
		int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
866

867
		if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds's avatar
Linus Torvalds committed
868 869 870
			copied = -EFAULT;
			break;
		}
871 872 873

		this_len = access_remote_vm(mm, addr, page, this_len, write);
		if (!this_len) {
Linus Torvalds's avatar
Linus Torvalds committed
874 875 876 877
			if (!copied)
				copied = -EIO;
			break;
		}
878 879 880 881 882 883 884 885 886 887

		if (!write && copy_to_user(buf, page, this_len)) {
			copied = -EFAULT;
			break;
		}

		buf += this_len;
		addr += this_len;
		copied += this_len;
		count -= this_len;
Linus Torvalds's avatar
Linus Torvalds committed
888
	}
889
	*ppos = addr;
890

891 892
	mmput(mm);
free:
893
	free_page((unsigned long) page);
Linus Torvalds's avatar
Linus Torvalds committed
894 895 896
	return copied;
}

897 898 899 900 901 902 903 904 905 906 907 908
static ssize_t mem_read(struct file *file, char __user *buf,
			size_t count, loff_t *ppos)
{
	return mem_rw(file, buf, count, ppos, 0);
}

static ssize_t mem_write(struct file *file, const char __user *buf,
			 size_t count, loff_t *ppos)
{
	return mem_rw(file, (char __user*)buf, count, ppos, 1);
}

909
loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds's avatar
Linus Torvalds committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
{
	switch (orig) {
	case 0:
		file->f_pos = offset;
		break;
	case 1:
		file->f_pos += offset;
		break;
	default:
		return -EINVAL;
	}
	force_successful_syscall_return();
	return file->f_pos;
}

925 926 927
static int mem_release(struct inode *inode, struct file *file)
{
	struct mm_struct *mm = file->private_data;
928
	if (mm)
929
		mmdrop(mm);
930 931 932
	return 0;
}

933
static const struct file_operations proc_mem_operations = {
Linus Torvalds's avatar
Linus Torvalds committed
934 935 936 937
	.llseek		= mem_lseek,
	.read		= mem_read,
	.write		= mem_write,
	.open		= mem_open,
938
	.release	= mem_release,
Linus Torvalds's avatar
Linus Torvalds committed
939 940
};

941 942 943 944 945
static int environ_open(struct inode *inode, struct file *file)
{
	return __mem_open(inode, file, PTRACE_MODE_READ);
}

946 947 948 949 950
static ssize_t environ_read(struct file *file, char __user *buf,
			size_t count, loff_t *ppos)
{
	char *page;
	unsigned long src = *ppos;
951 952
	int ret = 0;
	struct mm_struct *mm = file->private_data;
953
	unsigned long env_start, env_end;
954

955 956
	/* Ensure the process spawned far enough to have an environment. */
	if (!mm || !mm->env_end)
957
		return 0;
958 959 960

	page = (char *)__get_free_page(GFP_TEMPORARY);
	if (!page)
961
		return -ENOMEM;
962

Al Viro's avatar
Al Viro committed
963
	ret = 0;
964 965
	if (!atomic_inc_not_zero(&mm->mm_users))
		goto free;
966 967 968 969 970 971

	down_read(&mm->mmap_sem);
	env_start = mm->env_start;
	env_end = mm->env_end;
	up_read(&mm->mmap_sem);

972
	while (count > 0) {
973 974
		size_t this_len, max_len;
		int retval;
975

976
		if (src >= (env_end - env_start))
977 978
			break;

979
		this_len = env_end - (env_start + src);
980 981 982

		max_len = min_t(size_t, PAGE_SIZE, count);
		this_len = min(max_len, this_len);
983

984
		retval = access_remote_vm(mm, (env_start + src),
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
			page, this_len, 0);

		if (retval <= 0) {
			ret = retval;
			break;
		}

		if (copy_to_user(buf, page, retval)) {
			ret = -EFAULT;
			break;
		}

		ret += retval;
		src += retval;
		buf += retval;
		count -= retval;
	}
	*ppos = src;
	mmput(mm);
1004 1005

free:
1006 1007 1008 1009 1010
	free_page((unsigned long) page);
	return ret;
}

static const struct file_operations proc_environ_operations = {
1011
	.open		= environ_open,
1012
	.read		= environ_read,
1013
	.llseek		= generic_file_llseek,
1014
	.release	= mem_release,
1015 1016
};

1017 1018 1019
static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
			    loff_t *ppos)
{
Al Viro's avatar
Al Viro committed
1020
	struct task_struct *task = get_proc_task(file_inode(file));
1021 1022 1023 1024 1025 1026
	char buffer[PROC_NUMBUF];
	int oom_adj = OOM_ADJUST_MIN;
	size_t len;

	if (!task)
		return -ESRCH;
1027 1028 1029 1030 1031
	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
		oom_adj = OOM_ADJUST_MAX;
	else
		oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
			  OOM_SCORE_ADJ_MAX;
1032 1033 1034 1035 1036
	put_task_struct(task);
	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
	return simple_read_from_buffer(buf, count, ppos, buffer, len);
}

1037 1038 1039
static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
{
	static DEFINE_MUTEX(oom_adj_mutex);
1040
	struct mm_struct *mm = NULL;
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
	struct task_struct *task;
	int err = 0;

	task = get_proc_task(file_inode(file));
	if (!task)
		return -ESRCH;

	mutex_lock(&oom_adj_mutex);
	if (legacy) {
		if (oom_adj < task->signal->oom_score_adj &&
				!capable(CAP_SYS_RESOURCE)) {
			err = -EACCES;
			goto err_unlock;
		}
		/*
		 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
		 * /proc/pid/oom_score_adj instead.
		 */
		pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
			  current->comm, task_pid_nr(current), task_pid_nr(task),
			  task_pid_nr(task));
	} else {
		if ((short)oom_adj < task->signal->oom_score_adj_min &&
				!capable(CAP_SYS_RESOURCE)) {
			err = -EACCES;
			goto err_unlock;
		}
	}

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	/*
	 * Make sure we will check other processes sharing the mm if this is
	 * not vfrok which wants its own oom_score_adj.
	 * pin the mm so it doesn't go away and get reused after task_unlock
	 */
	if (!task->vfork_done) {
		struct task_struct *p = find_lock_task_mm(task);

		if (p) {
			if (atomic_read(&p->mm->mm_users) > 1) {
				mm = p->mm;
				atomic_inc(&mm->mm_count);
			}
			task_unlock(p);
		}
	}

1087 1088 1089 1090
	task->signal->oom_score_adj = oom_adj;
	if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
		task->signal->oom_score_adj_min = (short)oom_adj;
	trace_oom_score_adj_update(task);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118

	if (mm) {
		struct task_struct *p;

		rcu_read_lock();
		for_each_process(p) {
			if (same_thread_group(task, p))
				continue;

			/* do not touch kernel threads or the global init */
			if (p->flags & PF_KTHREAD || is_global_init(p))
				continue;

			task_lock(p);
			if (!p->vfork_done && process_shares_mm(p, mm)) {
				pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
						task_pid_nr(p), p->comm,
						p->signal->oom_score_adj, oom_adj,
						task_pid_nr(task), task->comm);
				p->signal->oom_score_adj = oom_adj;
				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
					p->signal->oom_score_adj_min = (short)oom_adj;
			}
			task_unlock(p);
		}
		rcu_read_unlock();
		mmdrop(mm);
	}
1119 1120 1121 1122 1123
err_unlock:
	mutex_unlock(&oom_adj_mutex);
	put_task_struct(task);
	return err;
}
1124

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
/*
 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
 * kernels.  The effective policy is defined by oom_score_adj, which has a
 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
 * Processes that become oom disabled via oom_adj will still be oom disabled
 * with this implementation.
 *
 * oom_adj cannot be removed since existing userspace binaries use it.
 */
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
static ssize_t oom_adj_write(struct file *file, const char __user *buf,
			     size_t count, loff_t *ppos)
{
	char buffer[PROC_NUMBUF];
	int oom_adj;
	int err;

	memset(buffer, 0, sizeof(buffer));
	if (count > sizeof(buffer) - 1)
		count = sizeof(buffer) - 1;
	if (copy_from_user(buffer, buf, count)) {
		err = -EFAULT;
		goto out;
	}

	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
	if (err)
		goto out;
	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
	     oom_adj != OOM_DISABLE) {
		err = -EINVAL;
		goto out;
	}

	/*
	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
	 * value is always attainable.
	 */
	if (oom_adj == OOM_ADJUST_MAX)
		oom_adj = OOM_SCORE_ADJ_MAX;
	else
		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;

1168
	err = __set_oom_adj(file, oom_adj, true);
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
out:
	return err < 0 ? err : count;
}

static const struct file_operations proc_oom_adj_operations = {
	.read		= oom_adj_read,
	.write		= oom_adj_write,
	.llseek		= generic_file_llseek,
};

1179 1180 1181
static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
					size_t count, loff_t *ppos)
{
Al Viro's avatar
Al Viro committed
1182
	struct task_struct *task = get_proc_task(file_inode(file));
1183
	char buffer[PROC_NUMBUF];
1184
	short oom_score_adj = OOM_SCORE_ADJ_MIN;
1185 1186 1187 1188
	size_t len;

	if (!task)
		return -ESRCH;
1189
	oom_score_adj = task->signal->oom_score_adj;
1190
	put_task_struct(task);
1191
	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1192 1193 1194 1195 1196 1197 1198
	return simple_read_from_buffer(buf, count, ppos, buffer, len);
}

static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
					size_t count, loff_t *ppos)
{
	char buffer[PROC_NUMBUF];
1199
	int oom_score_adj;
1200 1201 1202 1203 1204
	int err;

	memset(buffer, 0, sizeof(buffer));
	if (count > sizeof(buffer) - 1)
		count = sizeof(buffer) - 1;
1205 1206 1207 1208
	if (copy_from_user(buffer, buf, count)) {
		err = -EFAULT;
		goto out;
	}
1209

1210
	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1211
	if (err)
1212
		goto out;
1213
	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1214 1215 1216 1217
			oom_score_adj > OOM_SCORE_ADJ_MAX) {
		err = -EINVAL;
		goto out;
	}
1218

1219
	err = __set_oom_adj(file, oom_score_adj, false);
1220 1221
out:
	return err < 0 ? err : count;
1222 1223 1224 1225 1226
}

static const struct file_operations proc_oom_score_adj_operations = {
	.read		= oom_score_adj_read,
	.write		= oom_score_adj_write,
1227
	.llseek		= default_llseek,
1228 1229
};

Linus Torvalds's avatar
Linus Torvalds committed
1230 1231 1232 1233 1234
#ifdef CONFIG_AUDITSYSCALL
#define TMPBUFLEN 21
static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
				  size_t count, loff_t *ppos)
{
Al Viro's avatar
Al Viro committed
1235
	struct inode * inode = file_inode(file);
1236
	struct task_struct *task = get_proc_task(inode);
Linus Torvalds's avatar
Linus Torvalds committed
1237 1238 1239
	ssize_t length;
	char tmpbuf[TMPBUFLEN];

1240 1241
	if (!task)
		return -ESRCH;
Linus Torvalds's avatar
Linus Torvalds committed
1242
	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1243 1244
			   from_kuid(file->f_cred->user_ns,
				     audit_get_loginuid(task)));
1245
	put_task_struct(task);
Linus Torvalds's avatar
Linus Torvalds committed
1246 1247 1248 1249 1250 1251
	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
}

static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
				   size_t count, loff_t *ppos)
{
Al Viro's avatar
Al Viro committed
1252
	struct inode * inode = file_inode(file);
Linus Torvalds's avatar
Linus Torvalds committed
1253
	uid_t loginuid;
1254
	kuid_t kloginuid;
1255
	int rv;
Linus Torvalds's avatar
Linus Torvalds committed
1256

1257 1258 1259
	rcu_read_lock();
	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
		rcu_read_unlock();
Linus Torvalds's avatar
Linus Torvalds committed
1260
		return -EPERM;
1261 1262
	}
	rcu_read_unlock();
Linus Torvalds's avatar
Linus Torvalds committed
1263 1264 1265 1266 1267 1268

	if (*ppos != 0) {
		/* No partial writes. */
		return -EINVAL;
	}

1269 1270 1271
	rv = kstrtou32_from_user(buf, count, 10, &loginuid);
	if (rv < 0)
		return rv;
1272 1273 1274 1275 1276 1277

	/* is userspace tring to explicitly UNSET the loginuid? */
	if (loginuid == AUDIT_UID_UNSET) {
		kloginuid = INVALID_UID;
	} else {
		kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1278 1279
		if (!uid_valid(kloginuid))
			return -EINVAL;
1280 1281
	}

1282 1283 1284 1285
	rv = audit_set_loginuid(kloginuid);
	if (rv < 0)
		return rv;
	return count;
Linus Torvalds's avatar
Linus Torvalds committed
1286 1287
}

1288
static const struct file_operations proc_loginuid_operations = {
Linus Torvalds's avatar
Linus Torvalds committed
1289 1290
	.read		= proc_loginuid_read,
	.write		= proc_loginuid_write,
1291
	.llseek		= generic_file_llseek,
Linus Torvalds's avatar
Linus Torvalds committed
1292
};
1293 1294 1295 1296

static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
				  size_t count, loff_t *ppos)
{
Al Viro's avatar
Al Viro committed
1297
	struct inode * inode = file_inode(file);
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	struct task_struct *task = get_proc_task(inode);
	ssize_t length;
	char tmpbuf[TMPBUFLEN];

	if (!task)
		return -ESRCH;
	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
				audit_get_sessionid(task));
	put_task_struct(task);
	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
}

static const struct file_operations proc_sessionid_operations = {
	.read		= proc_sessionid_read,
1312
	.llseek		= generic_file_llseek,
1313
};
Linus Torvalds's avatar
Linus Torvalds committed
1314 1315
#endif

1316 1317 1318 1319
#ifdef CONFIG_FAULT_INJECTION
static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
				      size_t count, loff_t *ppos)
{
Al Viro's avatar
Al Viro committed
1320
	struct task_struct *task = get_proc_task(file_inode(file));
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	char buffer[PROC_NUMBUF];
	size_t len;
	int make_it_fail;

	if (!task)
		return -ESRCH;
	make_it_fail = task->make_it_fail;
	put_task_struct(task);

	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1331 1332

	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1333 1334 1335 1336 1337 1338
}

static ssize_t proc_fault_inject_write(struct file * file,
			const char __user * buf, size_t count, loff_t *ppos)
{
	struct task_struct *task;
1339
	char buffer[PROC_NUMBUF];
1340
	int make_it_fail;
1341
	int rv;
1342 1343 1344 1345 1346 1347 1348 1349

	if (!capable(CAP_SYS_RESOURCE))
		return -EPERM;
	memset(buffer, 0, sizeof(buffer));
	if (count > sizeof(buffer) - 1)
		count = sizeof(buffer) - 1;
	if (copy_from_user(buffer, buf, count))
		return -EFAULT;
1350 1351 1352
	rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
	if (rv < 0)
		return rv;
1353 1354 1355
	if (make_it_fail < 0 || make_it_fail > 1)
		return -EINVAL;

Al Viro's avatar
Al Viro committed
1356
	task = get_proc_task(file_inode(file));
1357 1358 1359 1360
	if (!task)
		return -ESRCH;
	task->make_it_fail = make_it_fail;
	put_task_struct(task);
1361 1362

	return count;
1363 1364
}

1365
static const struct file_operations proc_fault_inject_operations = {
1366 1367
	.read		= proc_fault_inject_read,
	.write		= proc_fault_inject_write,
1368
	.llseek		= generic_file_llseek,