Skip to content

Commit 8b985bb

Browse files
Tetsuo HandaTetsuo Handa
authored andcommitted
tomoyo: allow building as a loadable LSM module
One of concerns for enabling TOMOYO in prebuilt kernels is that distributor wants to avoid bloating kernel packages. Although boot-time kernel command line options allows selecting built-in LSMs to enable, file size increase of vmlinux and memory footprint increase of vmlinux caused by builtin-but- not-enabled LSMs remains. If it becomes possible to make LSMs dynamically appendable after boot using loadable kernel modules, these problems will go away. Another of concerns for enabling TOMOYO in prebuilt kernels is that who can provide support when distributor cannot provide support. Due to "those who compiled kernel code is expected to provide support for that kernel code" spell, TOMOYO is failing to get enabled in Fedora distribution [1]. The point of loadable kernel module is to share the workload. If it becomes possible to make LSMs dynamically appendable after boot using loadable kernel modules, as with people can use device drivers not supported by distributors but provided by third party device vendors, we can break this spell and can lower the barrier for using TOMOYO. This patch is intended for demonstrating that there is nothing difficult for supporting TOMOYO-like loadable LSM modules. For now we need to live with a mixture of built-in part and loadable part because fully loadable LSM modules are not supported since Linux 2.6.24 [2] and number of LSMs which can reserve static call slots is determined at compile time in Linux 6.12. Major changes in this patch are described below. There are no behavior changes as long as TOMOYO is built into vmlinux. Add CONFIG_SECURITY_TOMOYO_LKM as "bool" instead of changing CONFIG_SECURITY_TOMOYO from "bool" to "tristate", for something went wrong with how Makefile is evaluated if I choose "tristate". Add proxy.c for serving as a bridge between vmlinux and tomoyo.ko . Move callback functions from init.c to proxy.c when building as a loadable LSM module. init.c is built-in part and remains for reserving static call slots. proxy.c contains module's init function and tells init.c location of callback functions, making it possible to use static call for tomoyo.ko . By deferring initialization of "struct tomoyo_task" until tomoyo.ko is loaded, threads created between init.c reserved LSM hooks and proxy.c updates LSM hooks will have NULL "struct tomoyo_task" instances. Assuming that tomoyo.ko is loaded by the moment when the global init process starts, initialize "struct tomoyo_task" instance for current thread as a kernel thread when tomoyo_task(current) is called for the first time. There is a hack for exporting currently not-exported functions. This hack will be removed after all relevant functions are exported. Link: https://bugzilla.redhat.com/show_bug.cgi?id=542986 [1] Link: https://lkml.kernel.org/r/[email protected] [2] Signed-off-by: Tetsuo Handa <[email protected]>
1 parent 268225a commit 8b985bb

File tree

8 files changed

+467
-4
lines changed

8 files changed

+467
-4
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 init.o load_policy.o memory.o mount.o network.o realpath.o securityfs_if.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) */

0 commit comments

Comments
 (0)