Skip to content

Commit 5db84ab

Browse files
committed
act: use credential guards in acct_write_process()
Use credential guards for scoped credential override with automatic restoration on scope exit. Link: https://patch.msgid.link/20251103-work-creds-guards-simple-v1-14-a3e156839e7f@kernel.org Reviewed-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent c5c92c6 commit 5db84ab

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

kernel/acct.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -520,26 +520,23 @@ static void fill_ac(struct bsd_acct_struct *acct)
520520
static void acct_write_process(struct bsd_acct_struct *acct)
521521
{
522522
struct file *file = acct->file;
523-
const struct cred *cred;
524523
acct_t *ac = &acct->ac;
525524

526525
/* 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);
526+
scoped_with_creds(file->f_cred) {
527+
/*
528+
* First check to see if there is enough free_space to continue
529+
* the process accounting system. Then get freeze protection. If
530+
* the fs is frozen, just skip the write as we could deadlock
531+
* the system otherwise.
532+
*/
533+
if (check_free_space(acct) && file_start_write_trylock(file)) {
534+
/* it's been opened O_APPEND, so position is irrelevant */
535+
loff_t pos = 0;
536+
__kernel_write(file, ac, sizeof(acct_t), &pos);
537+
file_end_write(file);
538+
}
540539
}
541-
542-
revert_creds(cred);
543540
}
544541

545542
static void do_acct_process(struct bsd_acct_struct *acct)

0 commit comments

Comments
 (0)