Skip to content

Commit ba33a49

Browse files
committed
Merge tag 'tomoyo-pr-20240927' of git://git.code.sf.net/p/tomoyo/tomoyo
Pull tomoyo updates from Tetsuo Handa: "One bugfix patch, one preparation patch, and one conversion patch. TOMOYO is useful as an analysis tool for learning how a Linux system works. My boss was hoping that SELinux's policy is generated from what TOMOYO has observed. A translated paper describing it is available at https://master.dl.sourceforge.net/project/tomoyo/docs/nsf2003-en.pdf/nsf2003-en.pdf?viasf=1 Although that attempt failed due to mapping problem between inode and pathname, TOMOYO remains as an access restriction tool due to ability to write custom policy by individuals. I was delivering pure LKM version of TOMOYO (named AKARI) to users who cannot afford rebuilding their distro kernels with TOMOYO enabled. But since the LSM framework was converted to static calls, it became more difficult to deliver AKARI to such users. Therefore, I decided to update TOMOYO so that people can use mostly LKM version of TOMOYO with minimal burden for both distributors and users" * tag 'tomoyo-pr-20240927' of git://git.code.sf.net/p/tomoyo/tomoyo: tomoyo: fallback to realpath if symlink's pathname does not exist tomoyo: allow building as a loadable LSM module tomoyo: preparation step for building as a loadable LSM module
2 parents 033af36 + ada1986 commit ba33a49

File tree

12 files changed

+583
-121
lines changed

12 files changed

+583
-121
lines changed

security/tomoyo/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ config SECURITY_TOMOYO
1313
found at <https://tomoyo.sourceforge.net/>.
1414
If you are unsure how to answer this question, answer N.
1515

16+
config SECURITY_TOMOYO_LKM
17+
bool "Cut out most of TOMOYO's code to a loadable kernel module"
18+
default n
19+
depends on SECURITY_TOMOYO
20+
depends on MODULES
21+
help
22+
Say Y here if you want to include TOMOYO without bloating
23+
vmlinux file. If you say Y, most of TOMOYO code is cut out to
24+
a loadable kernel module named tomoyo.ko . This option will be
25+
useful for kernels built by Linux distributors where TOMOYO is
26+
included but TOMOYO is not enabled by default. Please be sure
27+
to explicitly load tomoyo.ko if you want to activate TOMOYO
28+
without calling userspace policy loader, for tomoyo.ko is
29+
loaded immediately before calling userspace policy loader.
30+
1631
config SECURITY_TOMOYO_MAX_ACCEPT_ENTRY
1732
int "Default maximal count for learning mode"
1833
default 2048

security/tomoyo/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# SPDX-License-Identifier: GPL-2.0
2-
obj-y = audit.o common.o condition.o domain.o environ.o file.o gc.o group.o load_policy.o memory.o mount.o network.o realpath.o securityfs_if.o tomoyo.o util.o
2+
tomoyo-objs := audit.o common.o condition.o domain.o environ.o file.o gc.o group.o memory.o mount.o network.o proxy.o realpath.o securityfs_if.o util.o
3+
obj-y += init.o load_policy.o
4+
ifdef CONFIG_SECURITY_TOMOYO_LKM
5+
obj-m += tomoyo.o
6+
else
7+
obj-y += tomoyo.o
8+
endif
39

410
targets += builtin-policy.h
511

security/tomoyo/common.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,13 @@ static bool tomoyo_select_domain(struct tomoyo_io_buffer *head,
998998
p = find_task_by_pid_ns(pid, &init_pid_ns);
999999
else
10001000
p = find_task_by_vpid(pid);
1001-
if (p)
1001+
if (p) {
10021002
domain = tomoyo_task(p)->domain_info;
1003+
#ifdef CONFIG_SECURITY_TOMOYO_LKM
1004+
if (!domain)
1005+
domain = &tomoyo_kernel_domain;
1006+
#endif
1007+
}
10031008
rcu_read_unlock();
10041009
} else if (!strncmp(data, "domain=", 7)) {
10051010
if (tomoyo_domain_def(data + 7))
@@ -1710,8 +1715,13 @@ static void tomoyo_read_pid(struct tomoyo_io_buffer *head)
17101715
p = find_task_by_pid_ns(pid, &init_pid_ns);
17111716
else
17121717
p = find_task_by_vpid(pid);
1713-
if (p)
1718+
if (p) {
17141719
domain = tomoyo_task(p)->domain_info;
1720+
#ifdef CONFIG_SECURITY_TOMOYO_LKM
1721+
if (!domain)
1722+
domain = &tomoyo_kernel_domain;
1723+
#endif
1724+
}
17151725
rcu_read_unlock();
17161726
if (!domain)
17171727
return;

security/tomoyo/common.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,7 @@ int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
978978
int tomoyo_init_request_info(struct tomoyo_request_info *r,
979979
struct tomoyo_domain_info *domain,
980980
const u8 index);
981+
int __init tomoyo_interface_init(void);
981982
int tomoyo_mkdev_perm(const u8 operation, const struct path *path,
982983
const unsigned int mode, unsigned int dev);
983984
int tomoyo_mount_permission(const char *dev_name, const struct path *path,
@@ -1214,10 +1215,14 @@ static inline void tomoyo_put_group(struct tomoyo_group *group)
12141215
*
12151216
* Returns pointer to "struct tomoyo_task" for specified thread.
12161217
*/
1218+
#ifdef CONFIG_SECURITY_TOMOYO_LKM
1219+
extern struct tomoyo_task *tomoyo_task(struct task_struct *task);
1220+
#else
12171221
static inline struct tomoyo_task *tomoyo_task(struct task_struct *task)
12181222
{
12191223
return task->security + tomoyo_blob_sizes.lbs_task;
12201224
}
1225+
#endif
12211226

12221227
/**
12231228
* tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
@@ -1284,4 +1289,71 @@ static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
12841289
pos = srcu_dereference((head)->next, &tomoyo_ss); \
12851290
for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
12861291

1292+
#ifdef CONFIG_SECURITY_TOMOYO_LKM
1293+
1294+
#define LSM_HOOK(RET, DEFAULT, NAME, ...) typedef RET (NAME##_t)(__VA_ARGS__);
1295+
#include <linux/lsm_hook_defs.h>
1296+
#undef LSM_HOOK
1297+
1298+
struct tomoyo_hooks {
1299+
cred_prepare_t *cred_prepare;
1300+
bprm_committed_creds_t *bprm_committed_creds;
1301+
task_alloc_t *task_alloc;
1302+
task_free_t *task_free;
1303+
bprm_check_security_t *bprm_check_security;
1304+
file_fcntl_t *file_fcntl;
1305+
file_open_t *file_open;
1306+
file_truncate_t *file_truncate;
1307+
path_truncate_t *path_truncate;
1308+
path_unlink_t *path_unlink;
1309+
path_mkdir_t *path_mkdir;
1310+
path_rmdir_t *path_rmdir;
1311+
path_symlink_t *path_symlink;
1312+
path_mknod_t *path_mknod;
1313+
path_link_t *path_link;
1314+
path_rename_t *path_rename;
1315+
inode_getattr_t *inode_getattr;
1316+
file_ioctl_t *file_ioctl;
1317+
file_ioctl_compat_t *file_ioctl_compat;
1318+
path_chmod_t *path_chmod;
1319+
path_chown_t *path_chown;
1320+
path_chroot_t *path_chroot;
1321+
sb_mount_t *sb_mount;
1322+
sb_umount_t *sb_umount;
1323+
sb_pivotroot_t *sb_pivotroot;
1324+
socket_bind_t *socket_bind;
1325+
socket_connect_t *socket_connect;
1326+
socket_listen_t *socket_listen;
1327+
socket_sendmsg_t *socket_sendmsg;
1328+
};
1329+
1330+
extern void tomoyo_register_hooks(const struct tomoyo_hooks *tomoyo_hooks);
1331+
1332+
struct tomoyo_operations {
1333+
void (*check_profile)(void);
1334+
int enabled;
1335+
};
1336+
1337+
extern struct tomoyo_operations tomoyo_ops;
1338+
1339+
/*
1340+
* Temporary hack: functions needed by tomoyo.ko . This will be removed
1341+
* after all functions are marked as EXPORT_STMBOL_GPL().
1342+
*/
1343+
struct tomoyo_tmp_exports {
1344+
struct task_struct * (*find_task_by_vpid)(pid_t nr);
1345+
struct task_struct * (*find_task_by_pid_ns)(pid_t nr, struct pid_namespace *ns);
1346+
void (*put_filesystem)(struct file_system_type *fs);
1347+
struct file * (*get_mm_exe_file)(struct mm_struct *mm);
1348+
char * (*d_absolute_path)(const struct path *path, char *buf, int buflen);
1349+
};
1350+
extern const struct tomoyo_tmp_exports tomoyo_tmp_exports;
1351+
#define find_task_by_vpid tomoyo_tmp_exports.find_task_by_vpid
1352+
#define find_task_by_pid_ns tomoyo_tmp_exports.find_task_by_pid_ns
1353+
#define put_filesystem tomoyo_tmp_exports.put_filesystem
1354+
#define get_mm_exe_file tomoyo_tmp_exports.get_mm_exe_file
1355+
#define d_absolute_path tomoyo_tmp_exports.d_absolute_path
1356+
1357+
#endif /* defined(CONFIG_SECURITY_TOMOYO_LKM) */
1358+
12871359
#endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */

security/tomoyo/domain.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,13 @@ int tomoyo_find_next_domain(struct linux_binprm *bprm)
723723
ee->r.obj = &ee->obj;
724724
ee->obj.path1 = bprm->file->f_path;
725725
/* Get symlink's pathname of program. */
726-
retval = -ENOENT;
727726
exename.name = tomoyo_realpath_nofollow(original_name);
728-
if (!exename.name)
729-
goto out;
727+
if (!exename.name) {
728+
/* Fallback to realpath if symlink's pathname does not exist. */
729+
exename.name = tomoyo_realpath_from_path(&bprm->file->f_path);
730+
if (!exename.name)
731+
goto out;
732+
}
730733
tomoyo_fill_path_info(&exename);
731734
retry:
732735
/* Check 'aggregator' directive. */

security/tomoyo/gc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <linux/kthread.h>
1010
#include <linux/slab.h>
1111

12+
/* Lock for GC. */
13+
DEFINE_SRCU(tomoyo_ss);
14+
1215
/**
1316
* tomoyo_memory_free - Free memory for elements.
1417
*

security/tomoyo/tomoyo.c renamed to security/tomoyo/hooks.h

Lines changed: 1 addition & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/*
3-
* security/tomoyo/tomoyo.c
3+
* security/tomoyo/hooks.h
44
*
55
* Copyright (C) 2005-2011 NTT DATA CORPORATION
66
*/
77

8-
#include <linux/lsm_hooks.h>
9-
#include <uapi/linux/lsm.h>
108
#include "common.h"
119

1210
/**
@@ -18,10 +16,6 @@ struct tomoyo_domain_info *tomoyo_domain(void)
1816
{
1917
struct tomoyo_task *s = tomoyo_task(current);
2018

21-
if (s->old_domain_info && !current->in_execve) {
22-
atomic_dec(&s->old_domain_info->users);
23-
s->old_domain_info = NULL;
24-
}
2519
return s->domain_info;
2620
}
2721

@@ -62,26 +56,6 @@ static void tomoyo_bprm_committed_creds(const struct linux_binprm *bprm)
6256
s->old_domain_info = NULL;
6357
}
6458

65-
#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
66-
/**
67-
* tomoyo_bprm_creds_for_exec - Target for security_bprm_creds_for_exec().
68-
*
69-
* @bprm: Pointer to "struct linux_binprm".
70-
*
71-
* Returns 0.
72-
*/
73-
static int tomoyo_bprm_creds_for_exec(struct linux_binprm *bprm)
74-
{
75-
/*
76-
* Load policy if /sbin/tomoyo-init exists and /sbin/init is requested
77-
* for the first time.
78-
*/
79-
if (!tomoyo_policy_loaded)
80-
tomoyo_load_policy(bprm->filename);
81-
return 0;
82-
}
83-
#endif
84-
8559
/**
8660
* tomoyo_bprm_check_security - Target for security_bprm_check().
8761
*
@@ -501,10 +475,6 @@ static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
501475
return tomoyo_socket_sendmsg_permission(sock, msg, size);
502476
}
503477

504-
struct lsm_blob_sizes tomoyo_blob_sizes __ro_after_init = {
505-
.lbs_task = sizeof(struct tomoyo_task),
506-
};
507-
508478
/**
509479
* tomoyo_task_alloc - Target for security_task_alloc().
510480
*
@@ -543,81 +513,3 @@ static void tomoyo_task_free(struct task_struct *task)
543513
s->old_domain_info = NULL;
544514
}
545515
}
546-
547-
static const struct lsm_id tomoyo_lsmid = {
548-
.name = "tomoyo",
549-
.id = LSM_ID_TOMOYO,
550-
};
551-
552-
/*
553-
* tomoyo_security_ops is a "struct security_operations" which is used for
554-
* registering TOMOYO.
555-
*/
556-
static struct security_hook_list tomoyo_hooks[] __ro_after_init = {
557-
LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
558-
LSM_HOOK_INIT(bprm_committed_creds, tomoyo_bprm_committed_creds),
559-
LSM_HOOK_INIT(task_alloc, tomoyo_task_alloc),
560-
LSM_HOOK_INIT(task_free, tomoyo_task_free),
561-
#ifndef CONFIG_SECURITY_TOMOYO_OMIT_USERSPACE_LOADER
562-
LSM_HOOK_INIT(bprm_creds_for_exec, tomoyo_bprm_creds_for_exec),
563-
#endif
564-
LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
565-
LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
566-
LSM_HOOK_INIT(file_open, tomoyo_file_open),
567-
LSM_HOOK_INIT(file_truncate, tomoyo_file_truncate),
568-
LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
569-
LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
570-
LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),
571-
LSM_HOOK_INIT(path_rmdir, tomoyo_path_rmdir),
572-
LSM_HOOK_INIT(path_symlink, tomoyo_path_symlink),
573-
LSM_HOOK_INIT(path_mknod, tomoyo_path_mknod),
574-
LSM_HOOK_INIT(path_link, tomoyo_path_link),
575-
LSM_HOOK_INIT(path_rename, tomoyo_path_rename),
576-
LSM_HOOK_INIT(inode_getattr, tomoyo_inode_getattr),
577-
LSM_HOOK_INIT(file_ioctl, tomoyo_file_ioctl),
578-
LSM_HOOK_INIT(file_ioctl_compat, tomoyo_file_ioctl),
579-
LSM_HOOK_INIT(path_chmod, tomoyo_path_chmod),
580-
LSM_HOOK_INIT(path_chown, tomoyo_path_chown),
581-
LSM_HOOK_INIT(path_chroot, tomoyo_path_chroot),
582-
LSM_HOOK_INIT(sb_mount, tomoyo_sb_mount),
583-
LSM_HOOK_INIT(sb_umount, tomoyo_sb_umount),
584-
LSM_HOOK_INIT(sb_pivotroot, tomoyo_sb_pivotroot),
585-
LSM_HOOK_INIT(socket_bind, tomoyo_socket_bind),
586-
LSM_HOOK_INIT(socket_connect, tomoyo_socket_connect),
587-
LSM_HOOK_INIT(socket_listen, tomoyo_socket_listen),
588-
LSM_HOOK_INIT(socket_sendmsg, tomoyo_socket_sendmsg),
589-
};
590-
591-
/* Lock for GC. */
592-
DEFINE_SRCU(tomoyo_ss);
593-
594-
int tomoyo_enabled __ro_after_init = 1;
595-
596-
/**
597-
* tomoyo_init - Register TOMOYO Linux as a LSM module.
598-
*
599-
* Returns 0.
600-
*/
601-
static int __init tomoyo_init(void)
602-
{
603-
struct tomoyo_task *s = tomoyo_task(current);
604-
605-
/* register ourselves with the security framework */
606-
security_add_hooks(tomoyo_hooks, ARRAY_SIZE(tomoyo_hooks),
607-
&tomoyo_lsmid);
608-
pr_info("TOMOYO Linux initialized\n");
609-
s->domain_info = &tomoyo_kernel_domain;
610-
atomic_inc(&tomoyo_kernel_domain.users);
611-
s->old_domain_info = NULL;
612-
tomoyo_mm_init();
613-
614-
return 0;
615-
}
616-
617-
DEFINE_LSM(tomoyo) = {
618-
.name = "tomoyo",
619-
.enabled = &tomoyo_enabled,
620-
.flags = LSM_FLAG_LEGACY_MAJOR,
621-
.blobs = &tomoyo_blob_sizes,
622-
.init = tomoyo_init,
623-
};

0 commit comments

Comments
 (0)