Skip to content

Commit 97fb35e

Browse files
Tetsuo Handaxjamesmorris
authored andcommitted
TOMOYO: Enable conditional ACL.
Enable conditional ACL by passing object's pointers. Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 5b63685 commit 97fb35e

5 files changed

Lines changed: 80 additions & 33 deletions

File tree

‎security/tomoyo/common.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@ int tomoyo_path2_perm(const u8 operation, struct path *path1,
836836
struct path *path2);
837837
int tomoyo_path_number_perm(const u8 operation, struct path *path,
838838
unsigned long number);
839-
int tomoyo_path_perm(const u8 operation, struct path *path);
839+
int tomoyo_path_perm(const u8 operation, struct path *path,
840+
const char *target);
840841
int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
841842
const struct tomoyo_path_info *filename);
842843
int tomoyo_poll_control(struct file *file, poll_table *wait);

‎security/tomoyo/domain.c‎

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -575,23 +575,27 @@ struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
575575
*/
576576
int tomoyo_find_next_domain(struct linux_binprm *bprm)
577577
{
578-
struct tomoyo_request_info r;
579-
char *tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
580578
struct tomoyo_domain_info *old_domain = tomoyo_domain();
581579
struct tomoyo_domain_info *domain = NULL;
582580
const char *original_name = bprm->filename;
583-
u8 mode;
584-
bool is_enforce;
585581
int retval = -ENOMEM;
586582
bool need_kfree = false;
587583
bool reject_on_transition_failure = false;
588584
struct tomoyo_path_info rn = { }; /* real name */
589-
590-
mode = tomoyo_init_request_info(&r, NULL, TOMOYO_MAC_FILE_EXECUTE);
591-
is_enforce = (mode == TOMOYO_CONFIG_ENFORCING);
592-
if (!tmp)
593-
goto out;
594-
585+
struct tomoyo_execve *ee = kzalloc(sizeof(*ee), GFP_NOFS);
586+
if (!ee)
587+
return -ENOMEM;
588+
ee->tmp = kzalloc(TOMOYO_EXEC_TMPSIZE, GFP_NOFS);
589+
if (!ee->tmp) {
590+
kfree(ee);
591+
return -ENOMEM;
592+
}
593+
/* ee->dump->data is allocated by tomoyo_dump_page(). */
594+
tomoyo_init_request_info(&ee->r, NULL, TOMOYO_MAC_FILE_EXECUTE);
595+
ee->r.ee = ee;
596+
ee->bprm = bprm;
597+
ee->r.obj = &ee->obj;
598+
ee->obj.path1 = bprm->file->f_path;
595599
retry:
596600
if (need_kfree) {
597601
kfree(rn.name);
@@ -625,7 +629,7 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
625629
}
626630

627631
/* Check execute permission. */
628-
retval = tomoyo_path_permission(&r, TOMOYO_TYPE_EXECUTE, &rn);
632+
retval = tomoyo_path_permission(&ee->r, TOMOYO_TYPE_EXECUTE, &rn);
629633
if (retval == TOMOYO_RETRY_REQUEST)
630634
goto retry;
631635
if (retval < 0)
@@ -636,20 +640,20 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
636640
* wildcard) rather than the pathname passed to execve()
637641
* (which never contains wildcard).
638642
*/
639-
if (r.param.path.matched_path) {
643+
if (ee->r.param.path.matched_path) {
640644
if (need_kfree)
641645
kfree(rn.name);
642646
need_kfree = false;
643647
/* This is OK because it is read only. */
644-
rn = *r.param.path.matched_path;
648+
rn = *ee->r.param.path.matched_path;
645649
}
646650

647651
/* Calculate domain to transit to. */
648652
switch (tomoyo_transition_type(old_domain->ns, old_domain->domainname,
649653
&rn)) {
650654
case TOMOYO_TRANSITION_CONTROL_RESET:
651655
/* Transit to the root of specified namespace. */
652-
snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>", rn.name);
656+
snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "<%s>", rn.name);
653657
/*
654658
* Make do_execve() fail if domain transition across namespaces
655659
* has failed.
@@ -658,7 +662,7 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
658662
break;
659663
case TOMOYO_TRANSITION_CONTROL_INITIALIZE:
660664
/* Transit to the child of current namespace's root. */
661-
snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
665+
snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
662666
old_domain->ns->name, rn.name);
663667
break;
664668
case TOMOYO_TRANSITION_CONTROL_KEEP:
@@ -677,29 +681,30 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
677681
domain = old_domain;
678682
} else {
679683
/* Normal domain transition. */
680-
snprintf(tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
684+
snprintf(ee->tmp, TOMOYO_EXEC_TMPSIZE - 1, "%s %s",
681685
old_domain->domainname->name, rn.name);
682686
}
683687
break;
684688
}
685689
if (!domain)
686-
domain = tomoyo_assign_domain(tmp, true);
690+
domain = tomoyo_assign_domain(ee->tmp, true);
687691
if (domain)
688692
retval = 0;
689693
else if (reject_on_transition_failure) {
690-
printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n", tmp);
694+
printk(KERN_WARNING "ERROR: Domain '%s' not ready.\n",
695+
ee->tmp);
691696
retval = -ENOMEM;
692-
} else if (r.mode == TOMOYO_CONFIG_ENFORCING)
697+
} else if (ee->r.mode == TOMOYO_CONFIG_ENFORCING)
693698
retval = -ENOMEM;
694699
else {
695700
retval = 0;
696701
if (!old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED]) {
697702
old_domain->flags[TOMOYO_DIF_TRANSITION_FAILED] = true;
698-
r.granted = false;
699-
tomoyo_write_log(&r, "%s", tomoyo_dif
703+
ee->r.granted = false;
704+
tomoyo_write_log(&ee->r, "%s", tomoyo_dif
700705
[TOMOYO_DIF_TRANSITION_FAILED]);
701706
printk(KERN_WARNING
702-
"ERROR: Domain '%s' not defined.\n", tmp);
707+
"ERROR: Domain '%s' not defined.\n", ee->tmp);
703708
}
704709
}
705710
out:
@@ -710,7 +715,9 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
710715
bprm->cred->security = domain;
711716
if (need_kfree)
712717
kfree(rn.name);
713-
kfree(tmp);
718+
kfree(ee->tmp);
719+
kfree(ee->dump.data);
720+
kfree(ee);
714721
return retval;
715722
}
716723

‎security/tomoyo/file.c‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,9 @@ int tomoyo_path_number_perm(const u8 type, struct path *path,
667667
unsigned long number)
668668
{
669669
struct tomoyo_request_info r;
670+
struct tomoyo_obj_info obj = {
671+
.path1 = *path,
672+
};
670673
int error = -ENOMEM;
671674
struct tomoyo_path_info buf;
672675
int idx;
@@ -677,6 +680,7 @@ int tomoyo_path_number_perm(const u8 type, struct path *path,
677680
idx = tomoyo_read_lock();
678681
if (!tomoyo_get_realpath(&buf, path))
679682
goto out;
683+
r.obj = &obj;
680684
if (type == TOMOYO_TYPE_MKDIR)
681685
tomoyo_add_slash(&buf);
682686
r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
@@ -711,6 +715,9 @@ int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
711715
int error = 0;
712716
struct tomoyo_path_info buf;
713717
struct tomoyo_request_info r;
718+
struct tomoyo_obj_info obj = {
719+
.path1 = *path,
720+
};
714721
int idx;
715722

716723
buf.name = NULL;
@@ -723,6 +730,7 @@ int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
723730
error = -ENOMEM;
724731
goto out;
725732
}
733+
r.obj = &obj;
726734
if (acc_mode & MAY_READ)
727735
error = tomoyo_path_permission(&r, TOMOYO_TYPE_READ,
728736
&buf);
@@ -745,15 +753,21 @@ int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
745753
*
746754
* @operation: Type of operation.
747755
* @path: Pointer to "struct path".
756+
* @target: Symlink's target if @operation is TOMOYO_TYPE_SYMLINK,
757+
* NULL otherwise.
748758
*
749759
* Returns 0 on success, negative value otherwise.
750760
*/
751-
int tomoyo_path_perm(const u8 operation, struct path *path)
761+
int tomoyo_path_perm(const u8 operation, struct path *path, const char *target)
752762
{
753763
struct tomoyo_request_info r;
764+
struct tomoyo_obj_info obj = {
765+
.path1 = *path,
766+
};
754767
int error;
755768
struct tomoyo_path_info buf;
756769
bool is_enforce;
770+
struct tomoyo_path_info symlink_target;
757771
int idx;
758772

759773
if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
@@ -765,13 +779,23 @@ int tomoyo_path_perm(const u8 operation, struct path *path)
765779
idx = tomoyo_read_lock();
766780
if (!tomoyo_get_realpath(&buf, path))
767781
goto out;
782+
r.obj = &obj;
768783
switch (operation) {
769784
case TOMOYO_TYPE_RMDIR:
770785
case TOMOYO_TYPE_CHROOT:
771786
tomoyo_add_slash(&buf);
772787
break;
788+
case TOMOYO_TYPE_SYMLINK:
789+
symlink_target.name = tomoyo_encode(target);
790+
if (!symlink_target.name)
791+
goto out;
792+
tomoyo_fill_path_info(&symlink_target);
793+
obj.symlink_target = &symlink_target;
794+
break;
773795
}
774796
error = tomoyo_path_permission(&r, operation, &buf);
797+
if (operation == TOMOYO_TYPE_SYMLINK)
798+
kfree(symlink_target.name);
775799
out:
776800
kfree(buf.name);
777801
tomoyo_read_unlock(idx);
@@ -794,6 +818,9 @@ int tomoyo_mkdev_perm(const u8 operation, struct path *path,
794818
const unsigned int mode, unsigned int dev)
795819
{
796820
struct tomoyo_request_info r;
821+
struct tomoyo_obj_info obj = {
822+
.path1 = *path,
823+
};
797824
int error = -ENOMEM;
798825
struct tomoyo_path_info buf;
799826
int idx;
@@ -804,6 +831,7 @@ int tomoyo_mkdev_perm(const u8 operation, struct path *path,
804831
idx = tomoyo_read_lock();
805832
error = -ENOMEM;
806833
if (tomoyo_get_realpath(&buf, path)) {
834+
r.obj = &obj;
807835
dev = new_decode_dev(dev);
808836
r.param_type = TOMOYO_TYPE_MKDEV_ACL;
809837
r.param.mkdev.filename = &buf;
@@ -837,6 +865,10 @@ int tomoyo_path2_perm(const u8 operation, struct path *path1,
837865
struct tomoyo_path_info buf1;
838866
struct tomoyo_path_info buf2;
839867
struct tomoyo_request_info r;
868+
struct tomoyo_obj_info obj = {
869+
.path1 = *path1,
870+
.path2 = *path2,
871+
};
840872
int idx;
841873

842874
if (tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
@@ -861,6 +893,7 @@ int tomoyo_path2_perm(const u8 operation, struct path *path1,
861893
tomoyo_add_slash(&buf2);
862894
break;
863895
}
896+
r.obj = &obj;
864897
r.param_type = TOMOYO_TYPE_PATH2_ACL;
865898
r.param.path2.operation = operation;
866899
r.param.path2.filename1 = &buf1;

‎security/tomoyo/mount.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
7575
struct path *dir, const char *type,
7676
unsigned long flags)
7777
{
78+
struct tomoyo_obj_info obj = { };
7879
struct path path;
7980
struct file_system_type *fstype = NULL;
8081
const char *requested_type = NULL;
@@ -85,6 +86,7 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
8586
struct tomoyo_path_info rdir;
8687
int need_dev = 0;
8788
int error = -ENOMEM;
89+
r->obj = &obj;
8890

8991
/* Get fstype. */
9092
requested_type = tomoyo_encode(type);
@@ -94,6 +96,7 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
9496
tomoyo_fill_path_info(&rtype);
9597

9698
/* Get mount point. */
99+
obj.path2 = *dir;
97100
requested_dir_name = tomoyo_realpath_from_path(dir);
98101
if (!requested_dir_name) {
99102
error = -ENOMEM;
@@ -129,8 +132,8 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
129132
error = -ENOENT;
130133
goto out;
131134
}
135+
obj.path1 = path;
132136
requested_dev_name = tomoyo_realpath_from_path(&path);
133-
path_put(&path);
134137
if (!requested_dev_name) {
135138
error = -ENOENT;
136139
goto out;
@@ -163,6 +166,9 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r, char *dev_name,
163166
if (fstype)
164167
put_filesystem(fstype);
165168
kfree(requested_type);
169+
/* Drop refcount obtained by kern_path(). */
170+
if (obj.path1.dentry)
171+
path_put(&obj.path1);
166172
return error;
167173
}
168174

‎security/tomoyo/tomoyo.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ static int tomoyo_bprm_check_security(struct linux_binprm *bprm)
9898
static int tomoyo_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
9999
{
100100
struct path path = { mnt, dentry };
101-
return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, &path);
101+
return tomoyo_path_perm(TOMOYO_TYPE_GETATTR, &path, NULL);
102102
}
103103

104104
static int tomoyo_path_truncate(struct path *path)
105105
{
106-
return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path);
106+
return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL);
107107
}
108108

109109
static int tomoyo_path_unlink(struct path *parent, struct dentry *dentry)
110110
{
111111
struct path path = { parent->mnt, dentry };
112-
return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path);
112+
return tomoyo_path_perm(TOMOYO_TYPE_UNLINK, &path, NULL);
113113
}
114114

115115
static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
@@ -123,14 +123,14 @@ static int tomoyo_path_mkdir(struct path *parent, struct dentry *dentry,
123123
static int tomoyo_path_rmdir(struct path *parent, struct dentry *dentry)
124124
{
125125
struct path path = { parent->mnt, dentry };
126-
return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path);
126+
return tomoyo_path_perm(TOMOYO_TYPE_RMDIR, &path, NULL);
127127
}
128128

129129
static int tomoyo_path_symlink(struct path *parent, struct dentry *dentry,
130130
const char *old_name)
131131
{
132132
struct path path = { parent->mnt, dentry };
133-
return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path);
133+
return tomoyo_path_perm(TOMOYO_TYPE_SYMLINK, &path, old_name);
134134
}
135135

136136
static int tomoyo_path_mknod(struct path *parent, struct dentry *dentry,
@@ -225,7 +225,7 @@ static int tomoyo_path_chown(struct path *path, uid_t uid, gid_t gid)
225225

226226
static int tomoyo_path_chroot(struct path *path)
227227
{
228-
return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path);
228+
return tomoyo_path_perm(TOMOYO_TYPE_CHROOT, path, NULL);
229229
}
230230

231231
static int tomoyo_sb_mount(char *dev_name, struct path *path,
@@ -237,7 +237,7 @@ static int tomoyo_sb_mount(char *dev_name, struct path *path,
237237
static int tomoyo_sb_umount(struct vfsmount *mnt, int flags)
238238
{
239239
struct path path = { mnt, mnt->mnt_root };
240-
return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path);
240+
return tomoyo_path_perm(TOMOYO_TYPE_UMOUNT, &path, NULL);
241241
}
242242

243243
static int tomoyo_sb_pivotroot(struct path *old_path, struct path *new_path)

0 commit comments

Comments
 (0)