1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/kernel/acct.c
4 *
5 * BSD Process Accounting for Linux
6 *
7 * Author: Marco van Wieringen <mvw@planets.elm.net>
8 *
9 * Some code based on ideas and code from:
10 * Thomas K. Dyas <tdyas@eden.rutgers.edu>
11 *
12 * This file implements BSD-style process accounting. Whenever any
13 * process exits, an accounting record of type "struct acct" is
14 * written to the file specified with the acct() system call. It is
15 * up to user-level programs to do useful things with the accounting
16 * log. The kernel just provides the raw accounting information.
17 *
18 * (C) Copyright 1995 - 1997 Marco van Wieringen - ELM Consultancy B.V.
19 *
20 * Plugged two leaks. 1) It didn't return acct_file into the free_filps if
21 * the file happened to be read-only. 2) If the accounting was suspended
22 * due to the lack of space it happily allowed to reopen it and completely
23 * lost the old acct_file. 3/10/98, Al Viro.
24 *
25 * Now we silently close acct_file on attempt to reopen. Cleaned sys_acct().
26 * XTerms and EMACS are manifestations of pure evil. 21/10/98, AV.
27 *
28 * Fixed a nasty interaction with sys_umount(). If the accounting
29 * was suspeneded we failed to stop it on umount(). Messy.
30 * Another one: remount to readonly didn't stop accounting.
31 * Question: what should we do if we have CAP_SYS_ADMIN but not
32 * CAP_SYS_PACCT? Current code does the following: umount returns -EBUSY
33 * unless we are messing with the root. In that case we are getting a
34 * real mess with do_remount_sb(). 9/11/98, AV.
35 *
36 * Fixed a bunch of races (and pair of leaks). Probably not the best way,
37 * but this one obviously doesn't introduce deadlocks. Later. BTW, found
38 * one race (and leak) in BSD implementation.
39 * OK, that's better. ANOTHER race and leak in BSD variant. There always
40 * is one more bug... 10/11/98, AV.
41 *
42 * Oh, fsck... Oopsable SMP race in do_process_acct() - we must hold
43 * ->mmap_lock to walk the vma list of current->mm. Nasty, since it leaks
44 * a struct file opened for write. Fixed. 2/6/2000, AV.
45 */
46
47 #include <linux/slab.h>
48 #include <linux/acct.h>
49 #include <linux/capability.h>
50 #include <linux/tty.h>
51 #include <linux/statfs.h>
52 #include <linux/jiffies.h>
53 #include <linux/syscalls.h>
54 #include <linux/namei.h>
55 #include <linux/sched/cputime.h>
56
57 #include <asm/div64.h>
58 #include <linux/pid_namespace.h>
59 #include <linux/fs_pin.h>
60
61 /*
62 * These constants control the amount of freespace that suspend and
63 * resume the process accounting system, and the time delay between
64 * each check.
65 * Turned into sysctl-controllable parameters. AV, 12/11/98
66 */
67
68 static int acct_parm[3] = {4, 2, 30};
69 #define RESUME (acct_parm[0]) /* >foo% free space - resume */
70 #define SUSPEND (acct_parm[1]) /* <foo% free space - suspend */
71 #define ACCT_TIMEOUT (acct_parm[2]) /* foo second timeout between checks */
72
73 #ifdef CONFIG_SYSCTL
74 static const struct ctl_table kern_acct_table[] = {
75 {
76 .procname = "acct",
77 .data = &acct_parm,
78 .maxlen = 3*sizeof(int),
79 .mode = 0644,
80 .proc_handler = proc_dointvec,
81 },
82 };
83
kernel_acct_sysctls_init(void)84 static __init int kernel_acct_sysctls_init(void)
85 {
86 register_sysctl_init("kernel", kern_acct_table);
87 return 0;
88 }
89 late_initcall(kernel_acct_sysctls_init);
90 #endif /* CONFIG_SYSCTL */
91
92 /*
93 * External references and all of the globals.
94 */
95
96 struct bsd_acct_struct {
97 struct fs_pin pin;
98 atomic_long_t count;
99 struct rcu_head rcu;
100 struct mutex lock;
101 bool active;
102 bool check_space;
103 unsigned long needcheck;
104 struct file *file;
105 struct pid_namespace *ns;
106 struct work_struct work;
107 struct completion done;
108 acct_t ac;
109 };
110
111 static void fill_ac(struct bsd_acct_struct *acct);
112 static void acct_write_process(struct bsd_acct_struct *acct);
113
114 /*
115 * Check the amount of free space and suspend/resume accordingly.
116 */
check_free_space(struct bsd_acct_struct * acct)117 static bool check_free_space(struct bsd_acct_struct *acct)
118 {
119 struct kstatfs sbuf;
120
121 if (!acct->check_space)
122 return acct->active;
123
124 /* May block */
125 if (vfs_statfs(&acct->file->f_path, &sbuf))
126 return acct->active;
127
128 if (acct->active) {
129 u64 suspend = sbuf.f_blocks * SUSPEND;
130 do_div(suspend, 100);
131 if (sbuf.f_bavail <= suspend) {
132 acct->active = false;
133 pr_info("Process accounting paused\n");
134 }
135 } else {
136 u64 resume = sbuf.f_blocks * RESUME;
137 do_div(resume, 100);
138 if (sbuf.f_bavail >= resume) {
139 acct->active = true;
140 pr_info("Process accounting resumed\n");
141 }
142 }
143
144 acct->needcheck = jiffies + ACCT_TIMEOUT*HZ;
145 return acct->active;
146 }
147
acct_put(struct bsd_acct_struct * p)148 static void acct_put(struct bsd_acct_struct *p)
149 {
150 if (atomic_long_dec_and_test(&p->count))
151 kfree_rcu(p, rcu);
152 }
153
to_acct(struct fs_pin * p)154 static inline struct bsd_acct_struct *to_acct(struct fs_pin *p)
155 {
156 return p ? container_of(p, struct bsd_acct_struct, pin) : NULL;
157 }
158
acct_get(struct pid_namespace * ns)159 static struct bsd_acct_struct *acct_get(struct pid_namespace *ns)
160 {
161 struct bsd_acct_struct *res;
162 again:
163 smp_rmb();
164 rcu_read_lock();
165 res = to_acct(READ_ONCE(ns->bacct));
166 if (!res) {
167 rcu_read_unlock();
168 return NULL;
169 }
170 if (!atomic_long_inc_not_zero(&res->count)) {
171 rcu_read_unlock();
172 cpu_relax();
173 goto again;
174 }
175 rcu_read_unlock();
176 mutex_lock(&res->lock);
177 if (res != to_acct(READ_ONCE(ns->bacct))) {
178 mutex_unlock(&res->lock);
179 acct_put(res);
180 goto again;
181 }
182 return res;
183 }
184
acct_pin_kill(struct fs_pin * pin)185 static void acct_pin_kill(struct fs_pin *pin)
186 {
187 struct bsd_acct_struct *acct = to_acct(pin);
188 mutex_lock(&acct->lock);
189 /*
190 * Fill the accounting struct with the exiting task's info
191 * before punting to the workqueue.
192 */
193 fill_ac(acct);
194 schedule_work(&acct->work);
195 wait_for_completion(&acct->done);
196 cmpxchg(&acct->ns->bacct, pin, NULL);
197 mutex_unlock(&acct->lock);
198 pin_remove(pin);
199 acct_put(acct);
200 }
201
close_work(struct work_struct * work)202 static void close_work(struct work_struct *work)
203 {
204 struct bsd_acct_struct *acct = container_of(work, struct bsd_acct_struct, work);
205 struct file *file = acct->file;
206
207 /* We were fired by acct_pin_kill() which holds acct->lock. */
208 acct_write_process(acct);
209 if (file->f_op->flush)
210 file->f_op->flush(file, NULL);
211 __fput_sync(file);
212 complete(&acct->done);
213 }
214
215 DEFINE_FREE(fput_sync, struct file *, if (!IS_ERR_OR_NULL(_T)) __fput_sync(_T))
acct_on(const char __user * name)216 static int acct_on(const char __user *name)
217 {
218 /* Difference from BSD - they don't do O_APPEND */
219 const int open_flags = O_WRONLY|O_APPEND|O_LARGEFILE;
220 struct pid_namespace *ns = task_active_pid_ns(current);
221 struct file *original_file __free(fput) = NULL; // in that order
222 struct path internal __free(path_put) = {}; // in that order
223 struct file *file __free(fput_sync) = NULL; // in that order
224 struct bsd_acct_struct *acct;
225 struct vfsmount *mnt;
226 struct fs_pin *old;
227
228 CLASS(filename, pathname)(name);
229 original_file = file_open_name(pathname, open_flags, 0);
230 if (IS_ERR(original_file))
231 return PTR_ERR(original_file);
232
233 mnt = mnt_clone_internal(&original_file->f_path);
234 if (IS_ERR(mnt))
235 return PTR_ERR(mnt);
236
237 internal.mnt = mnt;
238 internal.dentry = dget(mnt->mnt_root);
239
240 file = dentry_open(&internal, open_flags, current_cred());
241 if (IS_ERR(file))
242 return PTR_ERR(file);
243
244 if (!S_ISREG(file_inode(file)->i_mode))
245 return -EACCES;
246
247 /* Exclude kernel kernel internal filesystems. */
248 if (file_inode(file)->i_sb->s_flags & (SB_NOUSER | SB_KERNMOUNT))
249 return -EINVAL;
250
251 /* Exclude procfs and sysfs. */
252 if (file_inode(file)->i_sb->s_iflags & SB_I_USERNS_VISIBLE)
253 return -EINVAL;
254
255 if (!(file->f_mode & FMODE_CAN_WRITE))
256 return -EIO;
257
258 acct = kzalloc_obj(struct bsd_acct_struct);
259 if (!acct)
260 return -ENOMEM;
261
262 atomic_long_set(&acct->count, 1);
263 init_fs_pin(&acct->pin, acct_pin_kill);
264 acct->file = no_free_ptr(file);
265 acct->needcheck = jiffies;
266 acct->ns = ns;
267 mutex_init(&acct->lock);
268 INIT_WORK(&acct->work, close_work);
269 init_completion(&acct->done);
270 mutex_lock_nested(&acct->lock, 1); /* nobody has seen it yet */
271 pin_insert(&acct->pin, original_file->f_path.mnt);
272
273 rcu_read_lock();
274 old = xchg(&ns->bacct, &acct->pin);
275 mutex_unlock(&acct->lock);
276 pin_kill(old);
277 return 0;
278 }
279
280 static DEFINE_MUTEX(acct_on_mutex);
281
282 /**
283 * sys_acct - enable/disable process accounting
284 * @name: file name for accounting records or NULL to shutdown accounting
285 *
286 * sys_acct() is the only system call needed to implement process
287 * accounting. It takes the name of the file where accounting records
288 * should be written. If the filename is NULL, accounting will be
289 * shutdown.
290 *
291 * Returns: 0 for success or negative errno values for failure.
292 */
SYSCALL_DEFINE1(acct,const char __user *,name)293 SYSCALL_DEFINE1(acct, const char __user *, name)
294 {
295 int error = 0;
296
297 if (!capable(CAP_SYS_PACCT))
298 return -EPERM;
299
300 if (name) {
301 mutex_lock(&acct_on_mutex);
302 error = acct_on(name);
303 mutex_unlock(&acct_on_mutex);
304 } else {
305 rcu_read_lock();
306 pin_kill(task_active_pid_ns(current)->bacct);
307 }
308
309 return error;
310 }
311
acct_exit_ns(struct pid_namespace * ns)312 void acct_exit_ns(struct pid_namespace *ns)
313 {
314 rcu_read_lock();
315 pin_kill(ns->bacct);
316 }
317
318 /*
319 * encode an u64 into a comp_t
320 *
321 * This routine has been adopted from the encode_comp_t() function in
322 * the kern_acct.c file of the FreeBSD operating system. The encoding
323 * is a 13-bit fraction with a 3-bit (base 8) exponent.
324 */
325
326 #define MANTSIZE 13 /* 13 bit mantissa. */
327 #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
328 #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
329
encode_comp_t(u64 value)330 static comp_t encode_comp_t(u64 value)
331 {
332 int exp, rnd;
333
334 exp = rnd = 0;
335 while (value > MAXFRACT) {
336 rnd = value & (1 << (EXPSIZE - 1)); /* Round up? */
337 value >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
338 exp++;
339 }
340
341 /*
342 * If we need to round up, do it (and handle overflow correctly).
343 */
344 if (rnd && (++value > MAXFRACT)) {
345 value >>= EXPSIZE;
346 exp++;
347 }
348
349 if (exp > (((comp_t) ~0U) >> MANTSIZE))
350 return (comp_t) ~0U;
351 /*
352 * Clean it up and polish it off.
353 */
354 exp <<= MANTSIZE; /* Shift the exponent into place */
355 exp += value; /* and add on the mantissa. */
356 return exp;
357 }
358
359 #if ACCT_VERSION == 1 || ACCT_VERSION == 2
360 /*
361 * encode an u64 into a comp2_t (24 bits)
362 *
363 * Format: 5 bit base 2 exponent, 20 bits mantissa.
364 * The leading bit of the mantissa is not stored, but implied for
365 * non-zero exponents.
366 * Largest encodable value is 50 bits.
367 */
368
369 #define MANTSIZE2 20 /* 20 bit mantissa. */
370 #define EXPSIZE2 5 /* 5 bit base 2 exponent. */
371 #define MAXFRACT2 ((1ul << MANTSIZE2) - 1) /* Maximum fractional value. */
372 #define MAXEXP2 ((1 << EXPSIZE2) - 1) /* Maximum exponent. */
373
encode_comp2_t(u64 value)374 static comp2_t encode_comp2_t(u64 value)
375 {
376 int exp, rnd;
377
378 exp = (value > (MAXFRACT2>>1));
379 rnd = 0;
380 while (value > MAXFRACT2) {
381 rnd = value & 1;
382 value >>= 1;
383 exp++;
384 }
385
386 /*
387 * If we need to round up, do it (and handle overflow correctly).
388 */
389 if (rnd && (++value > MAXFRACT2)) {
390 value >>= 1;
391 exp++;
392 }
393
394 if (exp > MAXEXP2) {
395 /* Overflow. Return largest representable number instead. */
396 return (1ul << (MANTSIZE2+EXPSIZE2-1)) - 1;
397 } else {
398 return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1));
399 }
400 }
401 #elif ACCT_VERSION == 3
402 /*
403 * encode an u64 into a 32 bit IEEE float
404 */
encode_float(u64 value)405 static u32 encode_float(u64 value)
406 {
407 unsigned exp = 190;
408 unsigned u;
409
410 if (value == 0)
411 return 0;
412 while ((s64)value > 0) {
413 value <<= 1;
414 exp--;
415 }
416 u = (u32)(value >> 40) & 0x7fffffu;
417 return u | (exp << 23);
418 }
419 #endif
420
421 /*
422 * Write an accounting entry for an exiting process
423 *
424 * The acct_process() call is the workhorse of the process
425 * accounting system. The struct acct is built here and then written
426 * into the accounting file. This function should only be called from
427 * do_exit() or when switching to a different output file.
428 */
429
fill_ac(struct bsd_acct_struct * acct)430 static void fill_ac(struct bsd_acct_struct *acct)
431 {
432 struct pacct_struct *pacct = ¤t->signal->pacct;
433 struct file *file = acct->file;
434 acct_t *ac = &acct->ac;
435 u64 elapsed, run_time;
436 time64_t btime;
437 struct tty_struct *tty;
438
439 lockdep_assert_held(&acct->lock);
440
441 if (time_is_after_jiffies(acct->needcheck)) {
442 acct->check_space = false;
443
444 /* Don't fill in @ac if nothing will be written. */
445 if (!acct->active)
446 return;
447 } else {
448 acct->check_space = true;
449 }
450
451 /*
452 * Fill the accounting struct with the needed info as recorded
453 * by the different kernel functions.
454 */
455 memset(ac, 0, sizeof(acct_t));
456
457 ac->ac_version = ACCT_VERSION | ACCT_BYTEORDER;
458 strscpy(ac->ac_comm, current->comm, sizeof(ac->ac_comm));
459
460 /* calculate run_time in nsec*/
461 run_time = ktime_get_ns();
462 run_time -= current->group_leader->start_time;
463 /* convert nsec -> AHZ */
464 elapsed = nsec_to_AHZ(run_time);
465 #if ACCT_VERSION == 3
466 ac->ac_etime = encode_float(elapsed);
467 #else
468 ac->ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ?
469 (unsigned long) elapsed : (unsigned long) -1l);
470 #endif
471 #if ACCT_VERSION == 1 || ACCT_VERSION == 2
472 {
473 /* new enlarged etime field */
474 comp2_t etime = encode_comp2_t(elapsed);
475
476 ac->ac_etime_hi = etime >> 16;
477 ac->ac_etime_lo = (u16) etime;
478 }
479 #endif
480 do_div(elapsed, AHZ);
481 btime = ktime_get_real_seconds() - elapsed;
482 ac->ac_btime = clamp_t(time64_t, btime, 0, U32_MAX);
483 #if ACCT_VERSION == 2
484 ac->ac_ahz = AHZ;
485 #endif
486
487 spin_lock_irq(¤t->sighand->siglock);
488 tty = current->signal->tty; /* Safe as we hold the siglock */
489 ac->ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0;
490 ac->ac_utime = encode_comp_t(nsec_to_AHZ(pacct->ac_utime));
491 ac->ac_stime = encode_comp_t(nsec_to_AHZ(pacct->ac_stime));
492 ac->ac_flag = pacct->ac_flag;
493 ac->ac_mem = encode_comp_t(pacct->ac_mem);
494 ac->ac_minflt = encode_comp_t(pacct->ac_minflt);
495 ac->ac_majflt = encode_comp_t(pacct->ac_majflt);
496 ac->ac_exitcode = pacct->ac_exitcode;
497 spin_unlock_irq(¤t->sighand->siglock);
498
499 /* we really need to bite the bullet and change layout */
500 ac->ac_uid = from_kuid_munged(file->f_cred->user_ns, current_uid());
501 ac->ac_gid = from_kgid_munged(file->f_cred->user_ns, current_gid());
502 #if ACCT_VERSION == 1 || ACCT_VERSION == 2
503 /* backward-compatible 16 bit fields */
504 ac->ac_uid16 = ac->ac_uid;
505 ac->ac_gid16 = ac->ac_gid;
506 #elif ACCT_VERSION == 3
507 {
508 struct pid_namespace *ns = acct->ns;
509
510 ac->ac_pid = task_tgid_nr_ns(current, ns);
511 rcu_read_lock();
512 ac->ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent), ns);
513 rcu_read_unlock();
514 }
515 #endif
516 }
517
acct_write_process(struct bsd_acct_struct * acct)518 static void acct_write_process(struct bsd_acct_struct *acct)
519 {
520 struct file *file = acct->file;
521 acct_t *ac = &acct->ac;
522
523 /* Perform file operations on behalf of whoever enabled accounting */
524 scoped_with_creds(file->f_cred) {
525 /*
526 * First check to see if there is enough free_space to continue
527 * the process accounting system. Then get freeze protection. If
528 * the fs is frozen, just skip the write as we could deadlock
529 * the system otherwise.
530 */
531 if (check_free_space(acct) && file_start_write_trylock(file)) {
532 /* it's been opened O_APPEND, so position is irrelevant */
533 loff_t pos = 0;
534 __kernel_write(file, ac, sizeof(acct_t), &pos);
535 file_end_write(file);
536 }
537 }
538 }
539
do_acct_process(struct bsd_acct_struct * acct)540 static void do_acct_process(struct bsd_acct_struct *acct)
541 {
542 unsigned long flim;
543
544 /* Accounting records are not subject to resource limits. */
545 flim = rlimit(RLIMIT_FSIZE);
546 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
547 fill_ac(acct);
548 acct_write_process(acct);
549 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
550 }
551
552 /**
553 * acct_collect - collect accounting information into pacct_struct
554 * @exitcode: task exit code
555 * @group_dead: not 0, if this thread is the last one in the process.
556 */
acct_collect(long exitcode,int group_dead)557 void acct_collect(long exitcode, int group_dead)
558 {
559 struct pacct_struct *pacct = ¤t->signal->pacct;
560 u64 utime, stime;
561 unsigned long vsize = 0;
562
563 if (group_dead && current->mm) {
564 struct mm_struct *mm = current->mm;
565 VMA_ITERATOR(vmi, mm, 0);
566 struct vm_area_struct *vma;
567
568 mmap_read_lock(mm);
569 for_each_vma(vmi, vma)
570 vsize += vma->vm_end - vma->vm_start;
571 mmap_read_unlock(mm);
572 }
573
574 spin_lock_irq(¤t->sighand->siglock);
575 if (group_dead)
576 pacct->ac_mem = vsize / 1024;
577 if (thread_group_leader(current)) {
578 pacct->ac_exitcode = exitcode;
579 if (current->flags & PF_FORKNOEXEC)
580 pacct->ac_flag |= AFORK;
581 }
582 if (current->flags & PF_SUPERPRIV)
583 pacct->ac_flag |= ASU;
584 if (current->flags & PF_DUMPCORE)
585 pacct->ac_flag |= ACORE;
586 if (current->flags & PF_SIGNALED)
587 pacct->ac_flag |= AXSIG;
588
589 task_cputime(current, &utime, &stime);
590 pacct->ac_utime += utime;
591 pacct->ac_stime += stime;
592 pacct->ac_minflt += current->min_flt;
593 pacct->ac_majflt += current->maj_flt;
594 spin_unlock_irq(¤t->sighand->siglock);
595 }
596
slow_acct_process(struct pid_namespace * ns)597 static void slow_acct_process(struct pid_namespace *ns)
598 {
599 for ( ; ns; ns = ns->parent) {
600 struct bsd_acct_struct *acct = acct_get(ns);
601 if (acct) {
602 do_acct_process(acct);
603 mutex_unlock(&acct->lock);
604 acct_put(acct);
605 }
606 }
607 }
608
609 /**
610 * acct_process - handles process accounting for an exiting task
611 */
acct_process(void)612 void acct_process(void)
613 {
614 struct pid_namespace *ns;
615
616 /*
617 * This loop is safe lockless, since current is still
618 * alive and holds its namespace, which in turn holds
619 * its parent.
620 */
621 for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) {
622 if (ns->bacct)
623 break;
624 }
625 if (unlikely(ns))
626 slow_acct_process(ns);
627 }
628