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 filename *pathname __free(putname) = getname(name);
222 struct file *original_file __free(fput) = NULL; // in that order
223 struct path internal __free(path_put) = {}; // in that order
224 struct file *file __free(fput_sync) = NULL; // in that order
225 struct bsd_acct_struct *acct;
226 struct vfsmount *mnt;
227 struct fs_pin *old;
228
229 if (IS_ERR(pathname))
230 return PTR_ERR(pathname);
231 original_file = file_open_name(pathname, open_flags, 0);
232 if (IS_ERR(original_file))
233 return PTR_ERR(original_file);
234
235 mnt = mnt_clone_internal(&original_file->f_path);
236 if (IS_ERR(mnt))
237 return PTR_ERR(mnt);
238
239 internal.mnt = mnt;
240 internal.dentry = dget(mnt->mnt_root);
241
242 file = dentry_open(&internal, open_flags, current_cred());
243 if (IS_ERR(file))
244 return PTR_ERR(file);
245
246 if (!S_ISREG(file_inode(file)->i_mode))
247 return -EACCES;
248
249 /* Exclude kernel kernel internal filesystems. */
250 if (file_inode(file)->i_sb->s_flags & (SB_NOUSER | SB_KERNMOUNT))
251 return -EINVAL;
252
253 /* Exclude procfs and sysfs. */
254 if (file_inode(file)->i_sb->s_iflags & SB_I_USERNS_VISIBLE)
255 return -EINVAL;
256
257 if (!(file->f_mode & FMODE_CAN_WRITE))
258 return -EIO;
259
260 acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
261 if (!acct)
262 return -ENOMEM;
263
264 atomic_long_set(&acct->count, 1);
265 init_fs_pin(&acct->pin, acct_pin_kill);
266 acct->file = no_free_ptr(file);
267 acct->needcheck = jiffies;
268 acct->ns = ns;
269 mutex_init(&acct->lock);
270 INIT_WORK(&acct->work, close_work);
271 init_completion(&acct->done);
272 mutex_lock_nested(&acct->lock, 1); /* nobody has seen it yet */
273 pin_insert(&acct->pin, original_file->f_path.mnt);
274
275 rcu_read_lock();
276 old = xchg(&ns->bacct, &acct->pin);
277 mutex_unlock(&acct->lock);
278 pin_kill(old);
279 return 0;
280 }
281
282 static DEFINE_MUTEX(acct_on_mutex);
283
284 /**
285 * sys_acct - enable/disable process accounting
286 * @name: file name for accounting records or NULL to shutdown accounting
287 *
288 * sys_acct() is the only system call needed to implement process
289 * accounting. It takes the name of the file where accounting records
290 * should be written. If the filename is NULL, accounting will be
291 * shutdown.
292 *
293 * Returns: 0 for success or negative errno values for failure.
294 */
SYSCALL_DEFINE1(acct,const char __user *,name)295 SYSCALL_DEFINE1(acct, const char __user *, name)
296 {
297 int error = 0;
298
299 if (!capable(CAP_SYS_PACCT))
300 return -EPERM;
301
302 if (name) {
303 mutex_lock(&acct_on_mutex);
304 error = acct_on(name);
305 mutex_unlock(&acct_on_mutex);
306 } else {
307 rcu_read_lock();
308 pin_kill(task_active_pid_ns(current)->bacct);
309 }
310
311 return error;
312 }
313
acct_exit_ns(struct pid_namespace * ns)314 void acct_exit_ns(struct pid_namespace *ns)
315 {
316 rcu_read_lock();
317 pin_kill(ns->bacct);
318 }
319
320 /*
321 * encode an u64 into a comp_t
322 *
323 * This routine has been adopted from the encode_comp_t() function in
324 * the kern_acct.c file of the FreeBSD operating system. The encoding
325 * is a 13-bit fraction with a 3-bit (base 8) exponent.
326 */
327
328 #define MANTSIZE 13 /* 13 bit mantissa. */
329 #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
330 #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
331
encode_comp_t(u64 value)332 static comp_t encode_comp_t(u64 value)
333 {
334 int exp, rnd;
335
336 exp = rnd = 0;
337 while (value > MAXFRACT) {
338 rnd = value & (1 << (EXPSIZE - 1)); /* Round up? */
339 value >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
340 exp++;
341 }
342
343 /*
344 * If we need to round up, do it (and handle overflow correctly).
345 */
346 if (rnd && (++value > MAXFRACT)) {
347 value >>= EXPSIZE;
348 exp++;
349 }
350
351 if (exp > (((comp_t) ~0U) >> MANTSIZE))
352 return (comp_t) ~0U;
353 /*
354 * Clean it up and polish it off.
355 */
356 exp <<= MANTSIZE; /* Shift the exponent into place */
357 exp += value; /* and add on the mantissa. */
358 return exp;
359 }
360
361 #if ACCT_VERSION == 1 || ACCT_VERSION == 2
362 /*
363 * encode an u64 into a comp2_t (24 bits)
364 *
365 * Format: 5 bit base 2 exponent, 20 bits mantissa.
366 * The leading bit of the mantissa is not stored, but implied for
367 * non-zero exponents.
368 * Largest encodable value is 50 bits.
369 */
370
371 #define MANTSIZE2 20 /* 20 bit mantissa. */
372 #define EXPSIZE2 5 /* 5 bit base 2 exponent. */
373 #define MAXFRACT2 ((1ul << MANTSIZE2) - 1) /* Maximum fractional value. */
374 #define MAXEXP2 ((1 << EXPSIZE2) - 1) /* Maximum exponent. */
375
encode_comp2_t(u64 value)376 static comp2_t encode_comp2_t(u64 value)
377 {
378 int exp, rnd;
379
380 exp = (value > (MAXFRACT2>>1));
381 rnd = 0;
382 while (value > MAXFRACT2) {
383 rnd = value & 1;
384 value >>= 1;
385 exp++;
386 }
387
388 /*
389 * If we need to round up, do it (and handle overflow correctly).
390 */
391 if (rnd && (++value > MAXFRACT2)) {
392 value >>= 1;
393 exp++;
394 }
395
396 if (exp > MAXEXP2) {
397 /* Overflow. Return largest representable number instead. */
398 return (1ul << (MANTSIZE2+EXPSIZE2-1)) - 1;
399 } else {
400 return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1));
401 }
402 }
403 #elif ACCT_VERSION == 3
404 /*
405 * encode an u64 into a 32 bit IEEE float
406 */
encode_float(u64 value)407 static u32 encode_float(u64 value)
408 {
409 unsigned exp = 190;
410 unsigned u;
411
412 if (value == 0)
413 return 0;
414 while ((s64)value > 0) {
415 value <<= 1;
416 exp--;
417 }
418 u = (u32)(value >> 40) & 0x7fffffu;
419 return u | (exp << 23);
420 }
421 #endif
422
423 /*
424 * Write an accounting entry for an exiting process
425 *
426 * The acct_process() call is the workhorse of the process
427 * accounting system. The struct acct is built here and then written
428 * into the accounting file. This function should only be called from
429 * do_exit() or when switching to a different output file.
430 */
431
fill_ac(struct bsd_acct_struct * acct)432 static void fill_ac(struct bsd_acct_struct *acct)
433 {
434 struct pacct_struct *pacct = ¤t->signal->pacct;
435 struct file *file = acct->file;
436 acct_t *ac = &acct->ac;
437 u64 elapsed, run_time;
438 time64_t btime;
439 struct tty_struct *tty;
440
441 lockdep_assert_held(&acct->lock);
442
443 if (time_is_after_jiffies(acct->needcheck)) {
444 acct->check_space = false;
445
446 /* Don't fill in @ac if nothing will be written. */
447 if (!acct->active)
448 return;
449 } else {
450 acct->check_space = true;
451 }
452
453 /*
454 * Fill the accounting struct with the needed info as recorded
455 * by the different kernel functions.
456 */
457 memset(ac, 0, sizeof(acct_t));
458
459 ac->ac_version = ACCT_VERSION | ACCT_BYTEORDER;
460 strscpy(ac->ac_comm, current->comm, sizeof(ac->ac_comm));
461
462 /* calculate run_time in nsec*/
463 run_time = ktime_get_ns();
464 run_time -= current->group_leader->start_time;
465 /* convert nsec -> AHZ */
466 elapsed = nsec_to_AHZ(run_time);
467 #if ACCT_VERSION == 3
468 ac->ac_etime = encode_float(elapsed);
469 #else
470 ac->ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ?
471 (unsigned long) elapsed : (unsigned long) -1l);
472 #endif
473 #if ACCT_VERSION == 1 || ACCT_VERSION == 2
474 {
475 /* new enlarged etime field */
476 comp2_t etime = encode_comp2_t(elapsed);
477
478 ac->ac_etime_hi = etime >> 16;
479 ac->ac_etime_lo = (u16) etime;
480 }
481 #endif
482 do_div(elapsed, AHZ);
483 btime = ktime_get_real_seconds() - elapsed;
484 ac->ac_btime = clamp_t(time64_t, btime, 0, U32_MAX);
485 #if ACCT_VERSION == 2
486 ac->ac_ahz = AHZ;
487 #endif
488
489 spin_lock_irq(¤t->sighand->siglock);
490 tty = current->signal->tty; /* Safe as we hold the siglock */
491 ac->ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0;
492 ac->ac_utime = encode_comp_t(nsec_to_AHZ(pacct->ac_utime));
493 ac->ac_stime = encode_comp_t(nsec_to_AHZ(pacct->ac_stime));
494 ac->ac_flag = pacct->ac_flag;
495 ac->ac_mem = encode_comp_t(pacct->ac_mem);
496 ac->ac_minflt = encode_comp_t(pacct->ac_minflt);
497 ac->ac_majflt = encode_comp_t(pacct->ac_majflt);
498 ac->ac_exitcode = pacct->ac_exitcode;
499 spin_unlock_irq(¤t->sighand->siglock);
500
501 /* we really need to bite the bullet and change layout */
502 ac->ac_uid = from_kuid_munged(file->f_cred->user_ns, current_uid());
503 ac->ac_gid = from_kgid_munged(file->f_cred->user_ns, current_gid());
504 #if ACCT_VERSION == 1 || ACCT_VERSION == 2
505 /* backward-compatible 16 bit fields */
506 ac->ac_uid16 = ac->ac_uid;
507 ac->ac_gid16 = ac->ac_gid;
508 #elif ACCT_VERSION == 3
509 {
510 struct pid_namespace *ns = acct->ns;
511
512 ac->ac_pid = task_tgid_nr_ns(current, ns);
513 rcu_read_lock();
514 ac->ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent), ns);
515 rcu_read_unlock();
516 }
517 #endif
518 }
519
acct_write_process(struct bsd_acct_struct * acct)520 static void acct_write_process(struct bsd_acct_struct *acct)
521 {
522 struct file *file = acct->file;
523 const struct cred *cred;
524 acct_t *ac = &acct->ac;
525
526 /* Perform file operations on behalf of whoever enabled accounting */
527 cred = override_creds(file->f_cred);
528
529 /*
530 * First check to see if there is enough free_space to continue
531 * the process accounting system. Then get freeze protection. If
532 * the fs is frozen, just skip the write as we could deadlock
533 * the system otherwise.
534 */
535 if (check_free_space(acct) && file_start_write_trylock(file)) {
536 /* it's been opened O_APPEND, so position is irrelevant */
537 loff_t pos = 0;
538 __kernel_write(file, ac, sizeof(acct_t), &pos);
539 file_end_write(file);
540 }
541
542 revert_creds(cred);
543 }
544
do_acct_process(struct bsd_acct_struct * acct)545 static void do_acct_process(struct bsd_acct_struct *acct)
546 {
547 unsigned long flim;
548
549 /* Accounting records are not subject to resource limits. */
550 flim = rlimit(RLIMIT_FSIZE);
551 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
552 fill_ac(acct);
553 acct_write_process(acct);
554 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
555 }
556
557 /**
558 * acct_collect - collect accounting information into pacct_struct
559 * @exitcode: task exit code
560 * @group_dead: not 0, if this thread is the last one in the process.
561 */
acct_collect(long exitcode,int group_dead)562 void acct_collect(long exitcode, int group_dead)
563 {
564 struct pacct_struct *pacct = ¤t->signal->pacct;
565 u64 utime, stime;
566 unsigned long vsize = 0;
567
568 if (group_dead && current->mm) {
569 struct mm_struct *mm = current->mm;
570 VMA_ITERATOR(vmi, mm, 0);
571 struct vm_area_struct *vma;
572
573 mmap_read_lock(mm);
574 for_each_vma(vmi, vma)
575 vsize += vma->vm_end - vma->vm_start;
576 mmap_read_unlock(mm);
577 }
578
579 spin_lock_irq(¤t->sighand->siglock);
580 if (group_dead)
581 pacct->ac_mem = vsize / 1024;
582 if (thread_group_leader(current)) {
583 pacct->ac_exitcode = exitcode;
584 if (current->flags & PF_FORKNOEXEC)
585 pacct->ac_flag |= AFORK;
586 }
587 if (current->flags & PF_SUPERPRIV)
588 pacct->ac_flag |= ASU;
589 if (current->flags & PF_DUMPCORE)
590 pacct->ac_flag |= ACORE;
591 if (current->flags & PF_SIGNALED)
592 pacct->ac_flag |= AXSIG;
593
594 task_cputime(current, &utime, &stime);
595 pacct->ac_utime += utime;
596 pacct->ac_stime += stime;
597 pacct->ac_minflt += current->min_flt;
598 pacct->ac_majflt += current->maj_flt;
599 spin_unlock_irq(¤t->sighand->siglock);
600 }
601
slow_acct_process(struct pid_namespace * ns)602 static void slow_acct_process(struct pid_namespace *ns)
603 {
604 for ( ; ns; ns = ns->parent) {
605 struct bsd_acct_struct *acct = acct_get(ns);
606 if (acct) {
607 do_acct_process(acct);
608 mutex_unlock(&acct->lock);
609 acct_put(acct);
610 }
611 }
612 }
613
614 /**
615 * acct_process - handles process accounting for an exiting task
616 */
acct_process(void)617 void acct_process(void)
618 {
619 struct pid_namespace *ns;
620
621 /*
622 * This loop is safe lockless, since current is still
623 * alive and holds its namespace, which in turn holds
624 * its parent.
625 */
626 for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) {
627 if (ns->bacct)
628 break;
629 }
630 if (unlikely(ns))
631 slow_acct_process(ns);
632 }
633