Skip to content

Commit 89eda06

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
LSM: Add security_path_chmod() and security_path_chown().
This patch allows pathname based LSM modules to check chmod()/chown() operations. Since notify_change() does not receive "struct vfsmount *", we add security_path_chmod() and security_path_chown() to the caller of notify_change(). These hooks are used by TOMOYO. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 941fc5b commit 89eda06

4 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎fs/open.c‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,13 +616,17 @@ SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
616616
err = mnt_want_write_file(file);
617617
if (err)
618618
goto out_putf;
619+
err = security_path_chmod(dentry, file->f_vfsmnt, mode);
620+
if (err)
621+
goto out_drop_write;
619622
mutex_lock(&inode->i_mutex);
620623
if (mode == (mode_t) -1)
621624
mode = inode->i_mode;
622625
newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
623626
newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
624627
err = notify_change(dentry, &newattrs);
625628
mutex_unlock(&inode->i_mutex);
629+
out_drop_write:
626630
mnt_drop_write(file->f_path.mnt);
627631
out_putf:
628632
fput(file);
@@ -645,13 +649,17 @@ SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
645649
error = mnt_want_write(path.mnt);
646650
if (error)
647651
goto dput_and_out;
652+
error = security_path_chmod(path.dentry, path.mnt, mode);
653+
if (error)
654+
goto out_drop_write;
648655
mutex_lock(&inode->i_mutex);
649656
if (mode == (mode_t) -1)
650657
mode = inode->i_mode;
651658
newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
652659
newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
653660
error = notify_change(path.dentry, &newattrs);
654661
mutex_unlock(&inode->i_mutex);
662+
out_drop_write:
655663
mnt_drop_write(path.mnt);
656664
dput_and_out:
657665
path_put(&path);
@@ -700,7 +708,9 @@ SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
700708
error = mnt_want_write(path.mnt);
701709
if (error)
702710
goto out_release;
703-
error = chown_common(path.dentry, user, group);
711+
error = security_path_chown(&path, user, group);
712+
if (!error)
713+
error = chown_common(path.dentry, user, group);
704714
mnt_drop_write(path.mnt);
705715
out_release:
706716
path_put(&path);
@@ -725,7 +735,9 @@ SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
725735
error = mnt_want_write(path.mnt);
726736
if (error)
727737
goto out_release;
728-
error = chown_common(path.dentry, user, group);
738+
error = security_path_chown(&path, user, group);
739+
if (!error)
740+
error = chown_common(path.dentry, user, group);
729741
mnt_drop_write(path.mnt);
730742
out_release:
731743
path_put(&path);
@@ -744,7 +756,9 @@ SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group
744756
error = mnt_want_write(path.mnt);
745757
if (error)
746758
goto out_release;
747-
error = chown_common(path.dentry, user, group);
759+
error = security_path_chown(&path, user, group);
760+
if (!error)
761+
error = chown_common(path.dentry, user, group);
748762
mnt_drop_write(path.mnt);
749763
out_release:
750764
path_put(&path);
@@ -767,7 +781,9 @@ SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
767781
goto out_fput;
768782
dentry = file->f_path.dentry;
769783
audit_inode(NULL, dentry);
770-
error = chown_common(dentry, user, group);
784+
error = security_path_chown(&file->f_path, user, group);
785+
if (!error)
786+
error = chown_common(dentry, user, group);
771787
mnt_drop_write(file->f_path.mnt);
772788
out_fput:
773789
fput(file);

‎include/linux/security.h‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,18 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
447447
* @new_dir contains the path structure for parent of the new link.
448448
* @new_dentry contains the dentry structure of the new link.
449449
* Return 0 if permission is granted.
450+
* @path_chmod:
451+
* Check for permission to change DAC's permission of a file or directory.
452+
* @dentry contains the dentry structure.
453+
* @mnt contains the vfsmnt structure.
454+
* @mode contains DAC's mode.
455+
* Return 0 if permission is granted.
456+
* @path_chown:
457+
* Check for permission to change owner/group of a file or directory.
458+
* @path contains the path structure.
459+
* @uid contains new owner's ID.
460+
* @gid contains new group's ID.
461+
* Return 0 if permission is granted.
450462
* @inode_readlink:
451463
* Check the permission to read the symbolic link.
452464
* @dentry contains the dentry structure for the file link.
@@ -1488,6 +1500,9 @@ struct security_operations {
14881500
struct dentry *new_dentry);
14891501
int (*path_rename) (struct path *old_dir, struct dentry *old_dentry,
14901502
struct path *new_dir, struct dentry *new_dentry);
1503+
int (*path_chmod) (struct dentry *dentry, struct vfsmount *mnt,
1504+
mode_t mode);
1505+
int (*path_chown) (struct path *path, uid_t uid, gid_t gid);
14911506
#endif
14921507

14931508
int (*inode_alloc_security) (struct inode *inode);
@@ -2952,6 +2967,9 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
29522967
struct dentry *new_dentry);
29532968
int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
29542969
struct path *new_dir, struct dentry *new_dentry);
2970+
int security_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
2971+
mode_t mode);
2972+
int security_path_chown(struct path *path, uid_t uid, gid_t gid);
29552973
#else /* CONFIG_SECURITY_PATH */
29562974
static inline int security_path_unlink(struct path *dir, struct dentry *dentry)
29572975
{
@@ -3001,6 +3019,18 @@ static inline int security_path_rename(struct path *old_dir,
30013019
{
30023020
return 0;
30033021
}
3022+
3023+
static inline int security_path_chmod(struct dentry *dentry,
3024+
struct vfsmount *mnt,
3025+
mode_t mode)
3026+
{
3027+
return 0;
3028+
}
3029+
3030+
static inline int security_path_chown(struct path *path, uid_t uid, gid_t gid)
3031+
{
3032+
return 0;
3033+
}
30043034
#endif /* CONFIG_SECURITY_PATH */
30053035

30063036
#ifdef CONFIG_KEYS

‎security/capability.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,17 @@ static int cap_path_truncate(struct path *path, loff_t length,
308308
{
309309
return 0;
310310
}
311+
312+
static int cap_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
313+
mode_t mode)
314+
{
315+
return 0;
316+
}
317+
318+
static int cap_path_chown(struct path *path, uid_t uid, gid_t gid)
319+
{
320+
return 0;
321+
}
311322
#endif
312323

313324
static int cap_file_permission(struct file *file, int mask)
@@ -977,6 +988,8 @@ void security_fixup_ops(struct security_operations *ops)
977988
set_to_cap_if_null(ops, path_link);
978989
set_to_cap_if_null(ops, path_rename);
979990
set_to_cap_if_null(ops, path_truncate);
991+
set_to_cap_if_null(ops, path_chmod);
992+
set_to_cap_if_null(ops, path_chown);
980993
#endif
981994
set_to_cap_if_null(ops, file_permission);
982995
set_to_cap_if_null(ops, file_alloc_security);

‎security/security.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,21 @@ int security_path_truncate(struct path *path, loff_t length,
434434
return 0;
435435
return security_ops->path_truncate(path, length, time_attrs);
436436
}
437+
438+
int security_path_chmod(struct dentry *dentry, struct vfsmount *mnt,
439+
mode_t mode)
440+
{
441+
if (unlikely(IS_PRIVATE(dentry->d_inode)))
442+
return 0;
443+
return security_ops->path_chmod(dentry, mnt, mode);
444+
}
445+
446+
int security_path_chown(struct path *path, uid_t uid, gid_t gid)
447+
{
448+
if (unlikely(IS_PRIVATE(path->dentry->d_inode)))
449+
return 0;
450+
return security_ops->path_chown(path, uid, gid);
451+
}
437452
#endif
438453

439454
int security_inode_create(struct inode *dir, struct dentry *dentry, int mode)

0 commit comments

Comments
 (0)