Skip to content

Commit cdc59a6

Browse files
author
Al Viro
committed
kernel/acct.c: saner struct file treatment
Instead of switching ->f_path.mnt of an opened file to internal clone, get a struct path with ->mnt set to internal clone of that ->f_path.mnt, then dentry_open() that to get the file with right ->f_path.mnt from the very beginning. The only subtle part here is that on failure exits we need to close the file with __fput_sync() and make sure we do that *before* dropping the original mount. With that done, only fs/{file_table,open,namei}.c ever store anything to file->f_path and only prior to file->f_mode & FMODE_OPENED becoming true. Analysis of mount write count handling also becomes less brittle and convoluted... [AV: folded a fix for a bug spotted by Jan Kara - we do need a full-blown open of the original file, not just user_path_at() or we end up skipping permission checks] Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent b320789 commit cdc59a6

File tree

1 file changed

+36
-60
lines changed

1 file changed

+36
-60
lines changed

kernel/acct.c

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,14 @@
4444
* a struct file opened for write. Fixed. 2/6/2000, AV.
4545
*/
4646

47-
#include <linux/mm.h>
4847
#include <linux/slab.h>
4948
#include <linux/acct.h>
5049
#include <linux/capability.h>
51-
#include <linux/file.h>
5250
#include <linux/tty.h>
53-
#include <linux/security.h>
54-
#include <linux/vfs.h>
51+
#include <linux/statfs.h>
5552
#include <linux/jiffies.h>
56-
#include <linux/times.h>
5753
#include <linux/syscalls.h>
58-
#include <linux/mount.h>
59-
#include <linux/uaccess.h>
54+
#include <linux/namei.h>
6055
#include <linux/sched/cputime.h>
6156

6257
#include <asm/div64.h>
@@ -217,84 +212,70 @@ static void close_work(struct work_struct *work)
217212
complete(&acct->done);
218213
}
219214

220-
static int acct_on(struct filename *pathname)
215+
DEFINE_FREE(fput_sync, struct file *, if (!IS_ERR_OR_NULL(_T)) __fput_sync(_T))
216+
static int acct_on(const char __user *name)
221217
{
222-
struct file *file;
223-
struct vfsmount *mnt, *internal;
218+
/* Difference from BSD - they don't do O_APPEND */
219+
const int open_flags = O_WRONLY|O_APPEND|O_LARGEFILE;
224220
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
225225
struct bsd_acct_struct *acct;
226+
struct vfsmount *mnt;
226227
struct fs_pin *old;
227-
int err;
228228

229-
acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
230-
if (!acct)
231-
return -ENOMEM;
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);
232234

233-
/* Difference from BSD - they don't do O_APPEND */
234-
file = file_open_name(pathname, O_WRONLY|O_APPEND|O_LARGEFILE, 0);
235-
if (IS_ERR(file)) {
236-
kfree(acct);
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))
237244
return PTR_ERR(file);
238-
}
239245

240-
if (!S_ISREG(file_inode(file)->i_mode)) {
241-
kfree(acct);
242-
filp_close(file, NULL);
246+
if (!S_ISREG(file_inode(file)->i_mode))
243247
return -EACCES;
244-
}
245248

246249
/* Exclude kernel kernel internal filesystems. */
247-
if (file_inode(file)->i_sb->s_flags & (SB_NOUSER | SB_KERNMOUNT)) {
248-
kfree(acct);
249-
filp_close(file, NULL);
250+
if (file_inode(file)->i_sb->s_flags & (SB_NOUSER | SB_KERNMOUNT))
250251
return -EINVAL;
251-
}
252252

253253
/* Exclude procfs and sysfs. */
254-
if (file_inode(file)->i_sb->s_iflags & SB_I_USERNS_VISIBLE) {
255-
kfree(acct);
256-
filp_close(file, NULL);
254+
if (file_inode(file)->i_sb->s_iflags & SB_I_USERNS_VISIBLE)
257255
return -EINVAL;
258-
}
259256

260-
if (!(file->f_mode & FMODE_CAN_WRITE)) {
261-
kfree(acct);
262-
filp_close(file, NULL);
257+
if (!(file->f_mode & FMODE_CAN_WRITE))
263258
return -EIO;
264-
}
265-
internal = mnt_clone_internal(&file->f_path);
266-
if (IS_ERR(internal)) {
267-
kfree(acct);
268-
filp_close(file, NULL);
269-
return PTR_ERR(internal);
270-
}
271-
err = mnt_get_write_access(internal);
272-
if (err) {
273-
mntput(internal);
274-
kfree(acct);
275-
filp_close(file, NULL);
276-
return err;
277-
}
278-
mnt = file->f_path.mnt;
279-
file->f_path.mnt = internal;
259+
260+
acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
261+
if (!acct)
262+
return -ENOMEM;
280263

281264
atomic_long_set(&acct->count, 1);
282265
init_fs_pin(&acct->pin, acct_pin_kill);
283-
acct->file = file;
266+
acct->file = no_free_ptr(file);
284267
acct->needcheck = jiffies;
285268
acct->ns = ns;
286269
mutex_init(&acct->lock);
287270
INIT_WORK(&acct->work, close_work);
288271
init_completion(&acct->done);
289272
mutex_lock_nested(&acct->lock, 1); /* nobody has seen it yet */
290-
pin_insert(&acct->pin, mnt);
273+
pin_insert(&acct->pin, original_file->f_path.mnt);
291274

292275
rcu_read_lock();
293276
old = xchg(&ns->bacct, &acct->pin);
294277
mutex_unlock(&acct->lock);
295278
pin_kill(old);
296-
mnt_put_write_access(mnt);
297-
mntput(mnt);
298279
return 0;
299280
}
300281

@@ -319,14 +300,9 @@ SYSCALL_DEFINE1(acct, const char __user *, name)
319300
return -EPERM;
320301

321302
if (name) {
322-
struct filename *tmp = getname(name);
323-
324-
if (IS_ERR(tmp))
325-
return PTR_ERR(tmp);
326303
mutex_lock(&acct_on_mutex);
327-
error = acct_on(tmp);
304+
error = acct_on(name);
328305
mutex_unlock(&acct_on_mutex);
329-
putname(tmp);
330306
} else {
331307
rcu_read_lock();
332308
pin_kill(task_active_pid_ns(current)->bacct);

0 commit comments

Comments
 (0)