.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * AppArmor security module |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * |
---|
6 | 7 | * Copyright (C) 1998-2008 Novell/SUSE |
---|
7 | 8 | * Copyright 2009-2010 Canonical Ltd. |
---|
8 | | - * |
---|
9 | | - * This program is free software; you can redistribute it and/or |
---|
10 | | - * modify it under the terms of the GNU General Public License as |
---|
11 | | - * published by the Free Software Foundation, version 2 of the |
---|
12 | | - * License. |
---|
13 | 9 | */ |
---|
14 | 10 | |
---|
15 | 11 | #include <linux/lsm_hooks.h> |
---|
.. | .. |
---|
23 | 19 | #include <linux/sysctl.h> |
---|
24 | 20 | #include <linux/audit.h> |
---|
25 | 21 | #include <linux/user_namespace.h> |
---|
| 22 | +#include <linux/netfilter_ipv4.h> |
---|
| 23 | +#include <linux/netfilter_ipv6.h> |
---|
| 24 | +#include <linux/zlib.h> |
---|
26 | 25 | #include <net/sock.h> |
---|
| 26 | +#include <uapi/linux/mount.h> |
---|
27 | 27 | |
---|
28 | 28 | #include "include/apparmor.h" |
---|
29 | 29 | #include "include/apparmorfs.h" |
---|
.. | .. |
---|
44 | 44 | /* Flag indicating whether initialization completed */ |
---|
45 | 45 | int apparmor_initialized; |
---|
46 | 46 | |
---|
47 | | -DEFINE_PER_CPU(struct aa_buffers, aa_buffers); |
---|
48 | | -DEFINE_LOCAL_IRQ_LOCK(aa_buffers_lock); |
---|
| 47 | +union aa_buffer { |
---|
| 48 | + struct list_head list; |
---|
| 49 | + char buffer[1]; |
---|
| 50 | +}; |
---|
| 51 | + |
---|
| 52 | +#define RESERVE_COUNT 2 |
---|
| 53 | +static int reserve_count = RESERVE_COUNT; |
---|
| 54 | +static int buffer_count; |
---|
| 55 | + |
---|
| 56 | +static LIST_HEAD(aa_global_buffers); |
---|
| 57 | +static DEFINE_SPINLOCK(aa_buffers_lock); |
---|
49 | 58 | |
---|
50 | 59 | /* |
---|
51 | 60 | * LSM hook functions |
---|
.. | .. |
---|
57 | 66 | static void apparmor_cred_free(struct cred *cred) |
---|
58 | 67 | { |
---|
59 | 68 | aa_put_label(cred_label(cred)); |
---|
60 | | - cred_label(cred) = NULL; |
---|
| 69 | + set_cred_label(cred, NULL); |
---|
61 | 70 | } |
---|
62 | 71 | |
---|
63 | 72 | /* |
---|
.. | .. |
---|
65 | 74 | */ |
---|
66 | 75 | static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
---|
67 | 76 | { |
---|
68 | | - cred_label(cred) = NULL; |
---|
| 77 | + set_cred_label(cred, NULL); |
---|
69 | 78 | return 0; |
---|
70 | 79 | } |
---|
71 | 80 | |
---|
.. | .. |
---|
75 | 84 | static int apparmor_cred_prepare(struct cred *new, const struct cred *old, |
---|
76 | 85 | gfp_t gfp) |
---|
77 | 86 | { |
---|
78 | | - cred_label(new) = aa_get_newest_label(cred_label(old)); |
---|
| 87 | + set_cred_label(new, aa_get_newest_label(cred_label(old))); |
---|
79 | 88 | return 0; |
---|
80 | 89 | } |
---|
81 | 90 | |
---|
.. | .. |
---|
84 | 93 | */ |
---|
85 | 94 | static void apparmor_cred_transfer(struct cred *new, const struct cred *old) |
---|
86 | 95 | { |
---|
87 | | - cred_label(new) = aa_get_newest_label(cred_label(old)); |
---|
| 96 | + set_cred_label(new, aa_get_newest_label(cred_label(old))); |
---|
88 | 97 | } |
---|
89 | 98 | |
---|
90 | 99 | static void apparmor_task_free(struct task_struct *task) |
---|
91 | 100 | { |
---|
92 | 101 | |
---|
93 | 102 | aa_free_task_ctx(task_ctx(task)); |
---|
94 | | - task_ctx(task) = NULL; |
---|
95 | 103 | } |
---|
96 | 104 | |
---|
97 | 105 | static int apparmor_task_alloc(struct task_struct *task, |
---|
98 | 106 | unsigned long clone_flags) |
---|
99 | 107 | { |
---|
100 | | - struct aa_task_ctx *new = aa_alloc_task_ctx(GFP_KERNEL); |
---|
101 | | - |
---|
102 | | - if (!new) |
---|
103 | | - return -ENOMEM; |
---|
| 108 | + struct aa_task_ctx *new = task_ctx(task); |
---|
104 | 109 | |
---|
105 | 110 | aa_dup_task_ctx(new, task_ctx(current)); |
---|
106 | | - task_ctx(task) = new; |
---|
107 | 111 | |
---|
108 | 112 | return 0; |
---|
109 | 113 | } |
---|
.. | .. |
---|
431 | 435 | |
---|
432 | 436 | static int apparmor_file_alloc_security(struct file *file) |
---|
433 | 437 | { |
---|
434 | | - int error = 0; |
---|
435 | | - |
---|
436 | | - /* freed by apparmor_file_free_security */ |
---|
| 438 | + struct aa_file_ctx *ctx = file_ctx(file); |
---|
437 | 439 | struct aa_label *label = begin_current_label_crit_section(); |
---|
438 | | - file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL); |
---|
439 | | - if (!file_ctx(file)) |
---|
440 | | - error = -ENOMEM; |
---|
441 | | - end_current_label_crit_section(label); |
---|
442 | 440 | |
---|
443 | | - return error; |
---|
| 441 | + spin_lock_init(&ctx->lock); |
---|
| 442 | + rcu_assign_pointer(ctx->label, aa_get_label(label)); |
---|
| 443 | + end_current_label_crit_section(label); |
---|
| 444 | + return 0; |
---|
444 | 445 | } |
---|
445 | 446 | |
---|
446 | 447 | static void apparmor_file_free_security(struct file *file) |
---|
447 | 448 | { |
---|
448 | | - aa_free_file_ctx(file_ctx(file)); |
---|
| 449 | + struct aa_file_ctx *ctx = file_ctx(file); |
---|
| 450 | + |
---|
| 451 | + if (ctx) |
---|
| 452 | + aa_put_label(rcu_access_pointer(ctx->label)); |
---|
449 | 453 | } |
---|
450 | 454 | |
---|
451 | | -static int common_file_perm(const char *op, struct file *file, u32 mask) |
---|
| 455 | +static int common_file_perm(const char *op, struct file *file, u32 mask, |
---|
| 456 | + bool in_atomic) |
---|
452 | 457 | { |
---|
453 | 458 | struct aa_label *label; |
---|
454 | 459 | int error = 0; |
---|
.. | .. |
---|
458 | 463 | return -EACCES; |
---|
459 | 464 | |
---|
460 | 465 | label = __begin_current_label_crit_section(); |
---|
461 | | - error = aa_file_perm(op, label, file, mask); |
---|
| 466 | + error = aa_file_perm(op, label, file, mask, in_atomic); |
---|
462 | 467 | __end_current_label_crit_section(label); |
---|
463 | 468 | |
---|
464 | 469 | return error; |
---|
.. | .. |
---|
466 | 471 | |
---|
467 | 472 | static int apparmor_file_receive(struct file *file) |
---|
468 | 473 | { |
---|
469 | | - return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file)); |
---|
| 474 | + return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file), |
---|
| 475 | + false); |
---|
470 | 476 | } |
---|
471 | 477 | |
---|
472 | 478 | static int apparmor_file_permission(struct file *file, int mask) |
---|
473 | 479 | { |
---|
474 | | - return common_file_perm(OP_FPERM, file, mask); |
---|
| 480 | + return common_file_perm(OP_FPERM, file, mask, false); |
---|
475 | 481 | } |
---|
476 | 482 | |
---|
477 | 483 | static int apparmor_file_lock(struct file *file, unsigned int cmd) |
---|
.. | .. |
---|
481 | 487 | if (cmd == F_WRLCK) |
---|
482 | 488 | mask |= MAY_WRITE; |
---|
483 | 489 | |
---|
484 | | - return common_file_perm(OP_FLOCK, file, mask); |
---|
| 490 | + return common_file_perm(OP_FLOCK, file, mask, false); |
---|
485 | 491 | } |
---|
486 | 492 | |
---|
487 | 493 | static int common_mmap(const char *op, struct file *file, unsigned long prot, |
---|
488 | | - unsigned long flags) |
---|
| 494 | + unsigned long flags, bool in_atomic) |
---|
489 | 495 | { |
---|
490 | 496 | int mask = 0; |
---|
491 | 497 | |
---|
.. | .. |
---|
503 | 509 | if (prot & PROT_EXEC) |
---|
504 | 510 | mask |= AA_EXEC_MMAP; |
---|
505 | 511 | |
---|
506 | | - return common_file_perm(op, file, mask); |
---|
| 512 | + return common_file_perm(op, file, mask, in_atomic); |
---|
507 | 513 | } |
---|
508 | 514 | |
---|
509 | 515 | static int apparmor_mmap_file(struct file *file, unsigned long reqprot, |
---|
510 | 516 | unsigned long prot, unsigned long flags) |
---|
511 | 517 | { |
---|
512 | | - return common_mmap(OP_FMMAP, file, prot, flags); |
---|
| 518 | + return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC); |
---|
513 | 519 | } |
---|
514 | 520 | |
---|
515 | 521 | static int apparmor_file_mprotect(struct vm_area_struct *vma, |
---|
516 | 522 | unsigned long reqprot, unsigned long prot) |
---|
517 | 523 | { |
---|
518 | 524 | return common_mmap(OP_FMPROT, vma->vm_file, prot, |
---|
519 | | - !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0); |
---|
| 525 | + !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0, |
---|
| 526 | + false); |
---|
520 | 527 | } |
---|
521 | 528 | |
---|
522 | 529 | static int apparmor_sb_mount(const char *dev_name, const struct path *path, |
---|
.. | .. |
---|
732 | 739 | return error; |
---|
733 | 740 | } |
---|
734 | 741 | |
---|
735 | | -static int apparmor_task_kill(struct task_struct *target, struct siginfo *info, |
---|
| 742 | +static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info, |
---|
736 | 743 | int sig, const struct cred *cred) |
---|
737 | 744 | { |
---|
738 | 745 | struct aa_label *cl, *tl; |
---|
.. | .. |
---|
1025 | 1032 | return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); |
---|
1026 | 1033 | } |
---|
1027 | 1034 | |
---|
| 1035 | +#ifdef CONFIG_NETWORK_SECMARK |
---|
1028 | 1036 | /** |
---|
1029 | 1037 | * apparmor_socket_sock_recv_skb - check perms before associating skb to sk |
---|
1030 | 1038 | * |
---|
.. | .. |
---|
1035 | 1043 | */ |
---|
1036 | 1044 | static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
---|
1037 | 1045 | { |
---|
1038 | | - return 0; |
---|
| 1046 | + struct aa_sk_ctx *ctx = SK_CTX(sk); |
---|
| 1047 | + |
---|
| 1048 | + if (!skb->secmark) |
---|
| 1049 | + return 0; |
---|
| 1050 | + |
---|
| 1051 | + return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, |
---|
| 1052 | + skb->secmark, sk); |
---|
1039 | 1053 | } |
---|
| 1054 | +#endif |
---|
1040 | 1055 | |
---|
1041 | 1056 | |
---|
1042 | 1057 | static struct aa_label *sk_peer_label(struct sock *sk) |
---|
.. | .. |
---|
1131 | 1146 | ctx->label = aa_get_current_label(); |
---|
1132 | 1147 | } |
---|
1133 | 1148 | |
---|
| 1149 | +#ifdef CONFIG_NETWORK_SECMARK |
---|
| 1150 | +static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
---|
| 1151 | + struct request_sock *req) |
---|
| 1152 | +{ |
---|
| 1153 | + struct aa_sk_ctx *ctx = SK_CTX(sk); |
---|
| 1154 | + |
---|
| 1155 | + if (!skb->secmark) |
---|
| 1156 | + return 0; |
---|
| 1157 | + |
---|
| 1158 | + return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, |
---|
| 1159 | + skb->secmark, sk); |
---|
| 1160 | +} |
---|
| 1161 | +#endif |
---|
| 1162 | + |
---|
| 1163 | +/* |
---|
| 1164 | + * The cred blob is a pointer to, not an instance of, an aa_label. |
---|
| 1165 | + */ |
---|
| 1166 | +struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = { |
---|
| 1167 | + .lbs_cred = sizeof(struct aa_label *), |
---|
| 1168 | + .lbs_file = sizeof(struct aa_file_ctx), |
---|
| 1169 | + .lbs_task = sizeof(struct aa_task_ctx), |
---|
| 1170 | +}; |
---|
| 1171 | + |
---|
1134 | 1172 | static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { |
---|
1135 | 1173 | LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), |
---|
1136 | 1174 | LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), |
---|
.. | .. |
---|
1182 | 1220 | LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), |
---|
1183 | 1221 | LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), |
---|
1184 | 1222 | LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), |
---|
| 1223 | +#ifdef CONFIG_NETWORK_SECMARK |
---|
1185 | 1224 | LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), |
---|
| 1225 | +#endif |
---|
1186 | 1226 | LSM_HOOK_INIT(socket_getpeersec_stream, |
---|
1187 | 1227 | apparmor_socket_getpeersec_stream), |
---|
1188 | 1228 | LSM_HOOK_INIT(socket_getpeersec_dgram, |
---|
1189 | 1229 | apparmor_socket_getpeersec_dgram), |
---|
1190 | 1230 | LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), |
---|
| 1231 | +#ifdef CONFIG_NETWORK_SECMARK |
---|
| 1232 | + LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), |
---|
| 1233 | +#endif |
---|
1191 | 1234 | |
---|
1192 | 1235 | LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), |
---|
1193 | 1236 | LSM_HOOK_INIT(cred_free, apparmor_cred_free), |
---|
1194 | 1237 | LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), |
---|
1195 | 1238 | LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), |
---|
1196 | 1239 | |
---|
1197 | | - LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds), |
---|
| 1240 | + LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec), |
---|
1198 | 1241 | LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), |
---|
1199 | 1242 | LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), |
---|
1200 | 1243 | |
---|
.. | .. |
---|
1237 | 1280 | .get = param_get_aauint |
---|
1238 | 1281 | }; |
---|
1239 | 1282 | |
---|
| 1283 | +static int param_set_aacompressionlevel(const char *val, |
---|
| 1284 | + const struct kernel_param *kp); |
---|
| 1285 | +static int param_get_aacompressionlevel(char *buffer, |
---|
| 1286 | + const struct kernel_param *kp); |
---|
| 1287 | +#define param_check_aacompressionlevel param_check_int |
---|
| 1288 | +static const struct kernel_param_ops param_ops_aacompressionlevel = { |
---|
| 1289 | + .set = param_set_aacompressionlevel, |
---|
| 1290 | + .get = param_get_aacompressionlevel |
---|
| 1291 | +}; |
---|
| 1292 | + |
---|
1240 | 1293 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); |
---|
1241 | 1294 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); |
---|
1242 | 1295 | #define param_check_aalockpolicy param_check_bool |
---|
.. | .. |
---|
1266 | 1319 | #ifdef CONFIG_SECURITY_APPARMOR_HASH |
---|
1267 | 1320 | module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); |
---|
1268 | 1321 | #endif |
---|
| 1322 | + |
---|
| 1323 | +/* policy loaddata compression level */ |
---|
| 1324 | +int aa_g_rawdata_compression_level = Z_DEFAULT_COMPRESSION; |
---|
| 1325 | +module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level, |
---|
| 1326 | + aacompressionlevel, 0400); |
---|
1269 | 1327 | |
---|
1270 | 1328 | /* Debug mode */ |
---|
1271 | 1329 | bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES); |
---|
.. | .. |
---|
1307 | 1365 | bool aa_g_paranoid_load = true; |
---|
1308 | 1366 | module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); |
---|
1309 | 1367 | |
---|
| 1368 | +static int param_get_aaintbool(char *buffer, const struct kernel_param *kp); |
---|
| 1369 | +static int param_set_aaintbool(const char *val, const struct kernel_param *kp); |
---|
| 1370 | +#define param_check_aaintbool param_check_int |
---|
| 1371 | +static const struct kernel_param_ops param_ops_aaintbool = { |
---|
| 1372 | + .set = param_set_aaintbool, |
---|
| 1373 | + .get = param_get_aaintbool |
---|
| 1374 | +}; |
---|
1310 | 1375 | /* Boot time disable flag */ |
---|
1311 | | -static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE; |
---|
1312 | | -module_param_named(enabled, apparmor_enabled, bool, S_IRUGO); |
---|
| 1376 | +static int apparmor_enabled __lsm_ro_after_init = 1; |
---|
| 1377 | +module_param_named(enabled, apparmor_enabled, aaintbool, 0444); |
---|
1313 | 1378 | |
---|
1314 | 1379 | static int __init apparmor_enabled_setup(char *str) |
---|
1315 | 1380 | { |
---|
.. | .. |
---|
1370 | 1435 | return -EPERM; |
---|
1371 | 1436 | |
---|
1372 | 1437 | error = param_set_uint(val, kp); |
---|
| 1438 | + aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer)); |
---|
1373 | 1439 | pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); |
---|
1374 | 1440 | |
---|
1375 | 1441 | return error; |
---|
.. | .. |
---|
1382 | 1448 | if (apparmor_initialized && !policy_view_capable(NULL)) |
---|
1383 | 1449 | return -EPERM; |
---|
1384 | 1450 | return param_get_uint(buffer, kp); |
---|
| 1451 | +} |
---|
| 1452 | + |
---|
| 1453 | +/* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */ |
---|
| 1454 | +static int param_set_aaintbool(const char *val, const struct kernel_param *kp) |
---|
| 1455 | +{ |
---|
| 1456 | + struct kernel_param kp_local; |
---|
| 1457 | + bool value; |
---|
| 1458 | + int error; |
---|
| 1459 | + |
---|
| 1460 | + if (apparmor_initialized) |
---|
| 1461 | + return -EPERM; |
---|
| 1462 | + |
---|
| 1463 | + /* Create local copy, with arg pointing to bool type. */ |
---|
| 1464 | + value = !!*((int *)kp->arg); |
---|
| 1465 | + memcpy(&kp_local, kp, sizeof(kp_local)); |
---|
| 1466 | + kp_local.arg = &value; |
---|
| 1467 | + |
---|
| 1468 | + error = param_set_bool(val, &kp_local); |
---|
| 1469 | + if (!error) |
---|
| 1470 | + *((int *)kp->arg) = *((bool *)kp_local.arg); |
---|
| 1471 | + return error; |
---|
| 1472 | +} |
---|
| 1473 | + |
---|
| 1474 | +/* |
---|
| 1475 | + * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to |
---|
| 1476 | + * 1/0, this converts the "int that is actually bool" back to bool for |
---|
| 1477 | + * display in the /sys filesystem, while keeping it "int" for the LSM |
---|
| 1478 | + * infrastructure. |
---|
| 1479 | + */ |
---|
| 1480 | +static int param_get_aaintbool(char *buffer, const struct kernel_param *kp) |
---|
| 1481 | +{ |
---|
| 1482 | + struct kernel_param kp_local; |
---|
| 1483 | + bool value; |
---|
| 1484 | + |
---|
| 1485 | + /* Create local copy, with arg pointing to bool type. */ |
---|
| 1486 | + value = !!*((int *)kp->arg); |
---|
| 1487 | + memcpy(&kp_local, kp, sizeof(kp_local)); |
---|
| 1488 | + kp_local.arg = &value; |
---|
| 1489 | + |
---|
| 1490 | + return param_get_bool(buffer, &kp_local); |
---|
| 1491 | +} |
---|
| 1492 | + |
---|
| 1493 | +static int param_set_aacompressionlevel(const char *val, |
---|
| 1494 | + const struct kernel_param *kp) |
---|
| 1495 | +{ |
---|
| 1496 | + int error; |
---|
| 1497 | + |
---|
| 1498 | + if (!apparmor_enabled) |
---|
| 1499 | + return -EINVAL; |
---|
| 1500 | + if (apparmor_initialized) |
---|
| 1501 | + return -EPERM; |
---|
| 1502 | + |
---|
| 1503 | + error = param_set_int(val, kp); |
---|
| 1504 | + |
---|
| 1505 | + aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level, |
---|
| 1506 | + Z_NO_COMPRESSION, |
---|
| 1507 | + Z_BEST_COMPRESSION); |
---|
| 1508 | + pr_info("AppArmor: policy rawdata compression level set to %u\n", |
---|
| 1509 | + aa_g_rawdata_compression_level); |
---|
| 1510 | + |
---|
| 1511 | + return error; |
---|
| 1512 | +} |
---|
| 1513 | + |
---|
| 1514 | +static int param_get_aacompressionlevel(char *buffer, |
---|
| 1515 | + const struct kernel_param *kp) |
---|
| 1516 | +{ |
---|
| 1517 | + if (!apparmor_enabled) |
---|
| 1518 | + return -EINVAL; |
---|
| 1519 | + if (apparmor_initialized && !policy_view_capable(NULL)) |
---|
| 1520 | + return -EPERM; |
---|
| 1521 | + return param_get_int(buffer, kp); |
---|
1385 | 1522 | } |
---|
1386 | 1523 | |
---|
1387 | 1524 | static int param_get_audit(char *buffer, const struct kernel_param *kp) |
---|
.. | .. |
---|
1442 | 1579 | return 0; |
---|
1443 | 1580 | } |
---|
1444 | 1581 | |
---|
| 1582 | +char *aa_get_buffer(bool in_atomic) |
---|
| 1583 | +{ |
---|
| 1584 | + union aa_buffer *aa_buf; |
---|
| 1585 | + bool try_again = true; |
---|
| 1586 | + gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); |
---|
| 1587 | + |
---|
| 1588 | +retry: |
---|
| 1589 | + spin_lock(&aa_buffers_lock); |
---|
| 1590 | + if (buffer_count > reserve_count || |
---|
| 1591 | + (in_atomic && !list_empty(&aa_global_buffers))) { |
---|
| 1592 | + aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, |
---|
| 1593 | + list); |
---|
| 1594 | + list_del(&aa_buf->list); |
---|
| 1595 | + buffer_count--; |
---|
| 1596 | + spin_unlock(&aa_buffers_lock); |
---|
| 1597 | + return &aa_buf->buffer[0]; |
---|
| 1598 | + } |
---|
| 1599 | + if (in_atomic) { |
---|
| 1600 | + /* |
---|
| 1601 | + * out of reserve buffers and in atomic context so increase |
---|
| 1602 | + * how many buffers to keep in reserve |
---|
| 1603 | + */ |
---|
| 1604 | + reserve_count++; |
---|
| 1605 | + flags = GFP_ATOMIC; |
---|
| 1606 | + } |
---|
| 1607 | + spin_unlock(&aa_buffers_lock); |
---|
| 1608 | + |
---|
| 1609 | + if (!in_atomic) |
---|
| 1610 | + might_sleep(); |
---|
| 1611 | + aa_buf = kmalloc(aa_g_path_max, flags); |
---|
| 1612 | + if (!aa_buf) { |
---|
| 1613 | + if (try_again) { |
---|
| 1614 | + try_again = false; |
---|
| 1615 | + goto retry; |
---|
| 1616 | + } |
---|
| 1617 | + pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n"); |
---|
| 1618 | + return NULL; |
---|
| 1619 | + } |
---|
| 1620 | + return &aa_buf->buffer[0]; |
---|
| 1621 | +} |
---|
| 1622 | + |
---|
| 1623 | +void aa_put_buffer(char *buf) |
---|
| 1624 | +{ |
---|
| 1625 | + union aa_buffer *aa_buf; |
---|
| 1626 | + |
---|
| 1627 | + if (!buf) |
---|
| 1628 | + return; |
---|
| 1629 | + aa_buf = container_of(buf, union aa_buffer, buffer[0]); |
---|
| 1630 | + |
---|
| 1631 | + spin_lock(&aa_buffers_lock); |
---|
| 1632 | + list_add(&aa_buf->list, &aa_global_buffers); |
---|
| 1633 | + buffer_count++; |
---|
| 1634 | + spin_unlock(&aa_buffers_lock); |
---|
| 1635 | +} |
---|
| 1636 | + |
---|
1445 | 1637 | /* |
---|
1446 | 1638 | * AppArmor init functions |
---|
1447 | 1639 | */ |
---|
.. | .. |
---|
1453 | 1645 | */ |
---|
1454 | 1646 | static int __init set_init_ctx(void) |
---|
1455 | 1647 | { |
---|
1456 | | - struct cred *cred = (struct cred *)current->real_cred; |
---|
1457 | | - struct aa_task_ctx *ctx; |
---|
| 1648 | + struct cred *cred = (__force struct cred *)current->real_cred; |
---|
1458 | 1649 | |
---|
1459 | | - ctx = aa_alloc_task_ctx(GFP_KERNEL); |
---|
1460 | | - if (!ctx) |
---|
1461 | | - return -ENOMEM; |
---|
1462 | | - |
---|
1463 | | - cred_label(cred) = aa_get_label(ns_unconfined(root_ns)); |
---|
1464 | | - task_ctx(current) = ctx; |
---|
| 1650 | + set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); |
---|
1465 | 1651 | |
---|
1466 | 1652 | return 0; |
---|
1467 | 1653 | } |
---|
1468 | 1654 | |
---|
1469 | 1655 | static void destroy_buffers(void) |
---|
1470 | 1656 | { |
---|
1471 | | - u32 i, j; |
---|
| 1657 | + union aa_buffer *aa_buf; |
---|
1472 | 1658 | |
---|
1473 | | - for_each_possible_cpu(i) { |
---|
1474 | | - for_each_cpu_buffer(j) { |
---|
1475 | | - kfree(per_cpu(aa_buffers, i).buf[j]); |
---|
1476 | | - per_cpu(aa_buffers, i).buf[j] = NULL; |
---|
1477 | | - } |
---|
| 1659 | + spin_lock(&aa_buffers_lock); |
---|
| 1660 | + while (!list_empty(&aa_global_buffers)) { |
---|
| 1661 | + aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, |
---|
| 1662 | + list); |
---|
| 1663 | + list_del(&aa_buf->list); |
---|
| 1664 | + spin_unlock(&aa_buffers_lock); |
---|
| 1665 | + kfree(aa_buf); |
---|
| 1666 | + spin_lock(&aa_buffers_lock); |
---|
1478 | 1667 | } |
---|
| 1668 | + spin_unlock(&aa_buffers_lock); |
---|
1479 | 1669 | } |
---|
1480 | 1670 | |
---|
1481 | 1671 | static int __init alloc_buffers(void) |
---|
1482 | 1672 | { |
---|
1483 | | - u32 i, j; |
---|
| 1673 | + union aa_buffer *aa_buf; |
---|
| 1674 | + int i, num; |
---|
1484 | 1675 | |
---|
1485 | | - for_each_possible_cpu(i) { |
---|
1486 | | - for_each_cpu_buffer(j) { |
---|
1487 | | - char *buffer; |
---|
| 1676 | + /* |
---|
| 1677 | + * A function may require two buffers at once. Usually the buffers are |
---|
| 1678 | + * used for a short period of time and are shared. On UP kernel buffers |
---|
| 1679 | + * two should be enough, with more CPUs it is possible that more |
---|
| 1680 | + * buffers will be used simultaneously. The preallocated pool may grow. |
---|
| 1681 | + * This preallocation has also the side-effect that AppArmor will be |
---|
| 1682 | + * disabled early at boot if aa_g_path_max is extremly high. |
---|
| 1683 | + */ |
---|
| 1684 | + if (num_online_cpus() > 1) |
---|
| 1685 | + num = 4 + RESERVE_COUNT; |
---|
| 1686 | + else |
---|
| 1687 | + num = 2 + RESERVE_COUNT; |
---|
1488 | 1688 | |
---|
1489 | | - if (cpu_to_node(i) > num_online_nodes()) |
---|
1490 | | - /* fallback to kmalloc for offline nodes */ |
---|
1491 | | - buffer = kmalloc(aa_g_path_max, GFP_KERNEL); |
---|
1492 | | - else |
---|
1493 | | - buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL, |
---|
1494 | | - cpu_to_node(i)); |
---|
1495 | | - if (!buffer) { |
---|
1496 | | - destroy_buffers(); |
---|
1497 | | - return -ENOMEM; |
---|
1498 | | - } |
---|
1499 | | - per_cpu(aa_buffers, i).buf[j] = buffer; |
---|
| 1689 | + for (i = 0; i < num; i++) { |
---|
| 1690 | + |
---|
| 1691 | + aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL | |
---|
| 1692 | + __GFP_RETRY_MAYFAIL | __GFP_NOWARN); |
---|
| 1693 | + if (!aa_buf) { |
---|
| 1694 | + destroy_buffers(); |
---|
| 1695 | + return -ENOMEM; |
---|
1500 | 1696 | } |
---|
| 1697 | + aa_put_buffer(&aa_buf->buffer[0]); |
---|
1501 | 1698 | } |
---|
1502 | | - |
---|
1503 | 1699 | return 0; |
---|
1504 | 1700 | } |
---|
1505 | 1701 | |
---|
1506 | 1702 | #ifdef CONFIG_SYSCTL |
---|
1507 | 1703 | static int apparmor_dointvec(struct ctl_table *table, int write, |
---|
1508 | | - void __user *buffer, size_t *lenp, loff_t *ppos) |
---|
| 1704 | + void *buffer, size_t *lenp, loff_t *ppos) |
---|
1509 | 1705 | { |
---|
1510 | 1706 | if (!policy_admin_capable(NULL)) |
---|
1511 | 1707 | return -EPERM; |
---|
.. | .. |
---|
1543 | 1739 | } |
---|
1544 | 1740 | #endif /* CONFIG_SYSCTL */ |
---|
1545 | 1741 | |
---|
| 1742 | +#if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) |
---|
| 1743 | +static unsigned int apparmor_ip_postroute(void *priv, |
---|
| 1744 | + struct sk_buff *skb, |
---|
| 1745 | + const struct nf_hook_state *state) |
---|
| 1746 | +{ |
---|
| 1747 | + struct aa_sk_ctx *ctx; |
---|
| 1748 | + struct sock *sk; |
---|
| 1749 | + |
---|
| 1750 | + if (!skb->secmark) |
---|
| 1751 | + return NF_ACCEPT; |
---|
| 1752 | + |
---|
| 1753 | + sk = skb_to_full_sk(skb); |
---|
| 1754 | + if (sk == NULL) |
---|
| 1755 | + return NF_ACCEPT; |
---|
| 1756 | + |
---|
| 1757 | + ctx = SK_CTX(sk); |
---|
| 1758 | + if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, |
---|
| 1759 | + skb->secmark, sk)) |
---|
| 1760 | + return NF_ACCEPT; |
---|
| 1761 | + |
---|
| 1762 | + return NF_DROP_ERR(-ECONNREFUSED); |
---|
| 1763 | + |
---|
| 1764 | +} |
---|
| 1765 | + |
---|
| 1766 | +static unsigned int apparmor_ipv4_postroute(void *priv, |
---|
| 1767 | + struct sk_buff *skb, |
---|
| 1768 | + const struct nf_hook_state *state) |
---|
| 1769 | +{ |
---|
| 1770 | + return apparmor_ip_postroute(priv, skb, state); |
---|
| 1771 | +} |
---|
| 1772 | + |
---|
| 1773 | +#if IS_ENABLED(CONFIG_IPV6) |
---|
| 1774 | +static unsigned int apparmor_ipv6_postroute(void *priv, |
---|
| 1775 | + struct sk_buff *skb, |
---|
| 1776 | + const struct nf_hook_state *state) |
---|
| 1777 | +{ |
---|
| 1778 | + return apparmor_ip_postroute(priv, skb, state); |
---|
| 1779 | +} |
---|
| 1780 | +#endif |
---|
| 1781 | + |
---|
| 1782 | +static const struct nf_hook_ops apparmor_nf_ops[] = { |
---|
| 1783 | + { |
---|
| 1784 | + .hook = apparmor_ipv4_postroute, |
---|
| 1785 | + .pf = NFPROTO_IPV4, |
---|
| 1786 | + .hooknum = NF_INET_POST_ROUTING, |
---|
| 1787 | + .priority = NF_IP_PRI_SELINUX_FIRST, |
---|
| 1788 | + }, |
---|
| 1789 | +#if IS_ENABLED(CONFIG_IPV6) |
---|
| 1790 | + { |
---|
| 1791 | + .hook = apparmor_ipv6_postroute, |
---|
| 1792 | + .pf = NFPROTO_IPV6, |
---|
| 1793 | + .hooknum = NF_INET_POST_ROUTING, |
---|
| 1794 | + .priority = NF_IP6_PRI_SELINUX_FIRST, |
---|
| 1795 | + }, |
---|
| 1796 | +#endif |
---|
| 1797 | +}; |
---|
| 1798 | + |
---|
| 1799 | +static int __net_init apparmor_nf_register(struct net *net) |
---|
| 1800 | +{ |
---|
| 1801 | + int ret; |
---|
| 1802 | + |
---|
| 1803 | + ret = nf_register_net_hooks(net, apparmor_nf_ops, |
---|
| 1804 | + ARRAY_SIZE(apparmor_nf_ops)); |
---|
| 1805 | + return ret; |
---|
| 1806 | +} |
---|
| 1807 | + |
---|
| 1808 | +static void __net_exit apparmor_nf_unregister(struct net *net) |
---|
| 1809 | +{ |
---|
| 1810 | + nf_unregister_net_hooks(net, apparmor_nf_ops, |
---|
| 1811 | + ARRAY_SIZE(apparmor_nf_ops)); |
---|
| 1812 | +} |
---|
| 1813 | + |
---|
| 1814 | +static struct pernet_operations apparmor_net_ops = { |
---|
| 1815 | + .init = apparmor_nf_register, |
---|
| 1816 | + .exit = apparmor_nf_unregister, |
---|
| 1817 | +}; |
---|
| 1818 | + |
---|
| 1819 | +static int __init apparmor_nf_ip_init(void) |
---|
| 1820 | +{ |
---|
| 1821 | + int err; |
---|
| 1822 | + |
---|
| 1823 | + if (!apparmor_enabled) |
---|
| 1824 | + return 0; |
---|
| 1825 | + |
---|
| 1826 | + err = register_pernet_subsys(&apparmor_net_ops); |
---|
| 1827 | + if (err) |
---|
| 1828 | + panic("Apparmor: register_pernet_subsys: error %d\n", err); |
---|
| 1829 | + |
---|
| 1830 | + return 0; |
---|
| 1831 | +} |
---|
| 1832 | +__initcall(apparmor_nf_ip_init); |
---|
| 1833 | +#endif |
---|
| 1834 | + |
---|
1546 | 1835 | static int __init apparmor_init(void) |
---|
1547 | 1836 | { |
---|
1548 | 1837 | int error; |
---|
1549 | | - |
---|
1550 | | - if (!apparmor_enabled || !security_module_enable("apparmor")) { |
---|
1551 | | - aa_info_message("AppArmor disabled by boot time parameter"); |
---|
1552 | | - apparmor_enabled = false; |
---|
1553 | | - return 0; |
---|
1554 | | - } |
---|
1555 | 1838 | |
---|
1556 | 1839 | aa_secids_init(); |
---|
1557 | 1840 | |
---|
.. | .. |
---|
1577 | 1860 | error = alloc_buffers(); |
---|
1578 | 1861 | if (error) { |
---|
1579 | 1862 | AA_ERROR("Unable to allocate work buffers\n"); |
---|
1580 | | - goto buffers_out; |
---|
| 1863 | + goto alloc_out; |
---|
1581 | 1864 | } |
---|
1582 | 1865 | |
---|
1583 | 1866 | error = set_init_ctx(); |
---|
.. | .. |
---|
1602 | 1885 | |
---|
1603 | 1886 | buffers_out: |
---|
1604 | 1887 | destroy_buffers(); |
---|
1605 | | - |
---|
1606 | 1888 | alloc_out: |
---|
1607 | 1889 | aa_destroy_aafs(); |
---|
1608 | 1890 | aa_teardown_dfa_engine(); |
---|
.. | .. |
---|
1611 | 1893 | return error; |
---|
1612 | 1894 | } |
---|
1613 | 1895 | |
---|
1614 | | -security_initcall(apparmor_init); |
---|
| 1896 | +DEFINE_LSM(apparmor) = { |
---|
| 1897 | + .name = "apparmor", |
---|
| 1898 | + .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, |
---|
| 1899 | + .enabled = &apparmor_enabled, |
---|
| 1900 | + .blobs = &apparmor_blob_sizes, |
---|
| 1901 | + .init = apparmor_init, |
---|
| 1902 | +}; |
---|