hc
2024-05-10 cde9070d9970eef1f7ec2360586c802a16230ad8
kernel/kernel/auditsc.c
....@@ -75,6 +75,7 @@
7575 #include <linux/uaccess.h>
7676 #include <linux/fsnotify_backend.h>
7777 #include <uapi/linux/limits.h>
78
+#include <uapi/linux/netfilter/nf_tables.h>
7879
7980 #include "audit.h"
8081
....@@ -128,6 +129,34 @@
128129 struct audit_tree_refs {
129130 struct audit_tree_refs *next;
130131 struct audit_chunk *c[31];
132
+};
133
+
134
+struct audit_nfcfgop_tab {
135
+ enum audit_nfcfgop op;
136
+ const char *s;
137
+};
138
+
139
+static const struct audit_nfcfgop_tab audit_nfcfgs[] = {
140
+ { AUDIT_XT_OP_REGISTER, "xt_register" },
141
+ { AUDIT_XT_OP_REPLACE, "xt_replace" },
142
+ { AUDIT_XT_OP_UNREGISTER, "xt_unregister" },
143
+ { AUDIT_NFT_OP_TABLE_REGISTER, "nft_register_table" },
144
+ { AUDIT_NFT_OP_TABLE_UNREGISTER, "nft_unregister_table" },
145
+ { AUDIT_NFT_OP_CHAIN_REGISTER, "nft_register_chain" },
146
+ { AUDIT_NFT_OP_CHAIN_UNREGISTER, "nft_unregister_chain" },
147
+ { AUDIT_NFT_OP_RULE_REGISTER, "nft_register_rule" },
148
+ { AUDIT_NFT_OP_RULE_UNREGISTER, "nft_unregister_rule" },
149
+ { AUDIT_NFT_OP_SET_REGISTER, "nft_register_set" },
150
+ { AUDIT_NFT_OP_SET_UNREGISTER, "nft_unregister_set" },
151
+ { AUDIT_NFT_OP_SETELEM_REGISTER, "nft_register_setelem" },
152
+ { AUDIT_NFT_OP_SETELEM_UNREGISTER, "nft_unregister_setelem" },
153
+ { AUDIT_NFT_OP_GEN_REGISTER, "nft_register_gen" },
154
+ { AUDIT_NFT_OP_OBJ_REGISTER, "nft_register_obj" },
155
+ { AUDIT_NFT_OP_OBJ_UNREGISTER, "nft_unregister_obj" },
156
+ { AUDIT_NFT_OP_OBJ_RESET, "nft_reset_obj" },
157
+ { AUDIT_NFT_OP_FLOWTABLE_REGISTER, "nft_register_flowtable" },
158
+ { AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, "nft_unregister_flowtable" },
159
+ { AUDIT_NFT_OP_INVALID, "nft_invalid" },
131160 };
132161
133162 static int audit_match_perm(struct audit_context *ctx, int mask)
....@@ -200,7 +229,6 @@
200229 * References in it _are_ dropped - at the same time we free/drop aux stuff.
201230 */
202231
203
-#ifdef CONFIG_AUDIT_TREE
204232 static void audit_set_auditable(struct audit_context *ctx)
205233 {
206234 if (!ctx->prio) {
....@@ -245,12 +273,10 @@
245273 ctx->tree_count = 31;
246274 return 1;
247275 }
248
-#endif
249276
250277 static void unroll_tree_refs(struct audit_context *ctx,
251278 struct audit_tree_refs *p, int count)
252279 {
253
-#ifdef CONFIG_AUDIT_TREE
254280 struct audit_tree_refs *q;
255281 int n;
256282 if (!p) {
....@@ -274,7 +300,6 @@
274300 }
275301 ctx->trees = p;
276302 ctx->tree_count = count;
277
-#endif
278303 }
279304
280305 static void free_tree_refs(struct audit_context *ctx)
....@@ -288,7 +313,6 @@
288313
289314 static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
290315 {
291
-#ifdef CONFIG_AUDIT_TREE
292316 struct audit_tree_refs *p;
293317 int n;
294318 if (!tree)
....@@ -305,7 +329,6 @@
305329 if (audit_tree_match(p->c[n], tree))
306330 return 1;
307331 }
308
-#endif
309332 return 0;
310333 }
311334
....@@ -607,12 +630,20 @@
607630 }
608631 break;
609632 case AUDIT_WATCH:
610
- if (name)
611
- result = audit_watch_compare(rule->watch, name->ino, name->dev);
633
+ if (name) {
634
+ result = audit_watch_compare(rule->watch,
635
+ name->ino,
636
+ name->dev);
637
+ if (f->op == Audit_not_equal)
638
+ result = !result;
639
+ }
612640 break;
613641 case AUDIT_DIR:
614
- if (ctx)
642
+ if (ctx) {
615643 result = match_tree_refs(ctx, rule->tree);
644
+ if (f->op == Audit_not_equal)
645
+ result = !result;
646
+ }
616647 break;
617648 case AUDIT_LOGINUID:
618649 result = audit_uid_comparator(audit_get_loginuid(tsk),
....@@ -620,6 +651,11 @@
620651 break;
621652 case AUDIT_LOGINUID_SET:
622653 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
654
+ break;
655
+ case AUDIT_SADDR_FAM:
656
+ if (ctx && ctx->sockaddr)
657
+ result = audit_comparator(ctx->sockaddr->ss_family,
658
+ f->op, f->val);
623659 break;
624660 case AUDIT_SUBJ_USER:
625661 case AUDIT_SUBJ_ROLE:
....@@ -637,9 +673,8 @@
637673 need_sid = 0;
638674 }
639675 result = security_audit_rule_match(sid, f->type,
640
- f->op,
641
- f->lsm_rule,
642
- ctx);
676
+ f->op,
677
+ f->lsm_rule);
643678 }
644679 break;
645680 case AUDIT_OBJ_USER:
....@@ -653,13 +688,17 @@
653688 /* Find files that match */
654689 if (name) {
655690 result = security_audit_rule_match(
656
- name->osid, f->type, f->op,
657
- f->lsm_rule, ctx);
691
+ name->osid,
692
+ f->type,
693
+ f->op,
694
+ f->lsm_rule);
658695 } else if (ctx) {
659696 list_for_each_entry(n, &ctx->names_list, list) {
660
- if (security_audit_rule_match(n->osid, f->type,
661
- f->op, f->lsm_rule,
662
- ctx)) {
697
+ if (security_audit_rule_match(
698
+ n->osid,
699
+ f->type,
700
+ f->op,
701
+ f->lsm_rule)) {
663702 ++result;
664703 break;
665704 }
....@@ -670,7 +709,7 @@
670709 break;
671710 if (security_audit_rule_match(ctx->ipc.osid,
672711 f->type, f->op,
673
- f->lsm_rule, ctx))
712
+ f->lsm_rule))
674713 ++result;
675714 }
676715 break;
....@@ -687,9 +726,13 @@
687726 break;
688727 case AUDIT_PERM:
689728 result = audit_match_perm(ctx, f->val);
729
+ if (f->op == Audit_not_equal)
730
+ result = !result;
690731 break;
691732 case AUDIT_FILETYPE:
692733 result = audit_match_filetype(ctx, f->val);
734
+ if (f->op == Audit_not_equal)
735
+ result = !result;
693736 break;
694737 case AUDIT_FIELD_COMPARE:
695738 result = audit_field_compare(tsk, cred, f, ctx, name);
....@@ -774,15 +817,13 @@
774817 return AUDIT_DISABLED;
775818
776819 rcu_read_lock();
777
- if (!list_empty(list)) {
778
- list_for_each_entry_rcu(e, list, list) {
779
- if (audit_in_mask(&e->rule, ctx->major) &&
780
- audit_filter_rules(tsk, &e->rule, ctx, NULL,
781
- &state, false)) {
782
- rcu_read_unlock();
783
- ctx->current_state = state;
784
- return state;
785
- }
820
+ list_for_each_entry_rcu(e, list, list) {
821
+ if (audit_in_mask(&e->rule, ctx->major) &&
822
+ audit_filter_rules(tsk, &e->rule, ctx, NULL,
823
+ &state, false)) {
824
+ rcu_read_unlock();
825
+ ctx->current_state = state;
826
+ return state;
786827 }
787828 }
788829 rcu_read_unlock();
....@@ -801,9 +842,6 @@
801842 struct audit_entry *e;
802843 enum audit_state state;
803844
804
- if (list_empty(list))
805
- return 0;
806
-
807845 list_for_each_entry_rcu(e, list, list) {
808846 if (audit_in_mask(&e->rule, ctx->major) &&
809847 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
....@@ -811,7 +849,6 @@
811849 return 1;
812850 }
813851 }
814
-
815852 return 0;
816853 }
817854
....@@ -836,44 +873,6 @@
836873 rcu_read_unlock();
837874 }
838875
839
-/* Transfer the audit context pointer to the caller, clearing it in the tsk's struct */
840
-static inline struct audit_context *audit_take_context(struct task_struct *tsk,
841
- int return_valid,
842
- long return_code)
843
-{
844
- struct audit_context *context = tsk->audit_context;
845
-
846
- if (!context)
847
- return NULL;
848
- context->return_valid = return_valid;
849
-
850
- /*
851
- * we need to fix up the return code in the audit logs if the actual
852
- * return codes are later going to be fixed up by the arch specific
853
- * signal handlers
854
- *
855
- * This is actually a test for:
856
- * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
857
- * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
858
- *
859
- * but is faster than a bunch of ||
860
- */
861
- if (unlikely(return_code <= -ERESTARTSYS) &&
862
- (return_code >= -ERESTART_RESTARTBLOCK) &&
863
- (return_code != -ENOIOCTLCMD))
864
- context->return_code = -EINTR;
865
- else
866
- context->return_code = return_code;
867
-
868
- if (context->in_syscall && !context->dummy) {
869
- audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
870
- audit_filter_inodes(tsk, context);
871
- }
872
-
873
- audit_set_context(tsk, NULL);
874
- return context;
875
-}
876
-
877876 static inline void audit_proctitle_free(struct audit_context *context)
878877 {
879878 kfree(context->proctitle.value);
....@@ -881,6 +880,13 @@
881880 context->proctitle.len = 0;
882881 }
883882
883
+static inline void audit_free_module(struct audit_context *context)
884
+{
885
+ if (context->type == AUDIT_KERN_MODULE) {
886
+ kfree(context->module.name);
887
+ context->module.name = NULL;
888
+ }
889
+}
884890 static inline void audit_free_names(struct audit_context *context)
885891 {
886892 struct audit_names *n, *next;
....@@ -964,6 +970,7 @@
964970
965971 static inline void audit_free_context(struct audit_context *context)
966972 {
973
+ audit_free_module(context);
967974 audit_free_names(context);
968975 unroll_tree_refs(context, NULL, 0);
969976 free_tree_refs(context);
....@@ -1180,6 +1187,80 @@
11801187 kfree(buf_head);
11811188 }
11821189
1190
+static void audit_log_cap(struct audit_buffer *ab, char *prefix,
1191
+ kernel_cap_t *cap)
1192
+{
1193
+ int i;
1194
+
1195
+ if (cap_isclear(*cap)) {
1196
+ audit_log_format(ab, " %s=0", prefix);
1197
+ return;
1198
+ }
1199
+ audit_log_format(ab, " %s=", prefix);
1200
+ CAP_FOR_EACH_U32(i)
1201
+ audit_log_format(ab, "%08x", cap->cap[CAP_LAST_U32 - i]);
1202
+}
1203
+
1204
+static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1205
+{
1206
+ if (name->fcap_ver == -1) {
1207
+ audit_log_format(ab, " cap_fe=? cap_fver=? cap_fp=? cap_fi=?");
1208
+ return;
1209
+ }
1210
+ audit_log_cap(ab, "cap_fp", &name->fcap.permitted);
1211
+ audit_log_cap(ab, "cap_fi", &name->fcap.inheritable);
1212
+ audit_log_format(ab, " cap_fe=%d cap_fver=%x cap_frootid=%d",
1213
+ name->fcap.fE, name->fcap_ver,
1214
+ from_kuid(&init_user_ns, name->fcap.rootid));
1215
+}
1216
+
1217
+static void audit_log_time(struct audit_context *context, struct audit_buffer **ab)
1218
+{
1219
+ const struct audit_ntp_data *ntp = &context->time.ntp_data;
1220
+ const struct timespec64 *tk = &context->time.tk_injoffset;
1221
+ static const char * const ntp_name[] = {
1222
+ "offset",
1223
+ "freq",
1224
+ "status",
1225
+ "tai",
1226
+ "tick",
1227
+ "adjust",
1228
+ };
1229
+ int type;
1230
+
1231
+ if (context->type == AUDIT_TIME_ADJNTPVAL) {
1232
+ for (type = 0; type < AUDIT_NTP_NVALS; type++) {
1233
+ if (ntp->vals[type].newval != ntp->vals[type].oldval) {
1234
+ if (!*ab) {
1235
+ *ab = audit_log_start(context,
1236
+ GFP_KERNEL,
1237
+ AUDIT_TIME_ADJNTPVAL);
1238
+ if (!*ab)
1239
+ return;
1240
+ }
1241
+ audit_log_format(*ab, "op=%s old=%lli new=%lli",
1242
+ ntp_name[type],
1243
+ ntp->vals[type].oldval,
1244
+ ntp->vals[type].newval);
1245
+ audit_log_end(*ab);
1246
+ *ab = NULL;
1247
+ }
1248
+ }
1249
+ }
1250
+ if (tk->tv_sec != 0 || tk->tv_nsec != 0) {
1251
+ if (!*ab) {
1252
+ *ab = audit_log_start(context, GFP_KERNEL,
1253
+ AUDIT_TIME_INJOFFSET);
1254
+ if (!*ab)
1255
+ return;
1256
+ }
1257
+ audit_log_format(*ab, "sec=%lli nsec=%li",
1258
+ (long long)tk->tv_sec, tk->tv_nsec);
1259
+ audit_log_end(*ab);
1260
+ *ab = NULL;
1261
+ }
1262
+}
1263
+
11831264 static void show_special(struct audit_context *context, int *call_panic)
11841265 {
11851266 struct audit_buffer *ab;
....@@ -1281,10 +1362,14 @@
12811362 audit_log_format(ab, "name=");
12821363 if (context->module.name) {
12831364 audit_log_untrustedstring(ab, context->module.name);
1284
- kfree(context->module.name);
12851365 } else
12861366 audit_log_format(ab, "(null)");
12871367
1368
+ break;
1369
+ case AUDIT_TIME_ADJNTPVAL:
1370
+ case AUDIT_TIME_INJOFFSET:
1371
+ /* this call deviates from the rest, eating the buffer */
1372
+ audit_log_time(context, &ab);
12881373 break;
12891374 }
12901375 audit_log_end(ab);
....@@ -1302,14 +1387,108 @@
13021387 return len;
13031388 }
13041389
1305
-static void audit_log_proctitle(struct task_struct *tsk,
1306
- struct audit_context *context)
1390
+/*
1391
+ * audit_log_name - produce AUDIT_PATH record from struct audit_names
1392
+ * @context: audit_context for the task
1393
+ * @n: audit_names structure with reportable details
1394
+ * @path: optional path to report instead of audit_names->name
1395
+ * @record_num: record number to report when handling a list of names
1396
+ * @call_panic: optional pointer to int that will be updated if secid fails
1397
+ */
1398
+static void audit_log_name(struct audit_context *context, struct audit_names *n,
1399
+ const struct path *path, int record_num, int *call_panic)
1400
+{
1401
+ struct audit_buffer *ab;
1402
+
1403
+ ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1404
+ if (!ab)
1405
+ return;
1406
+
1407
+ audit_log_format(ab, "item=%d", record_num);
1408
+
1409
+ if (path)
1410
+ audit_log_d_path(ab, " name=", path);
1411
+ else if (n->name) {
1412
+ switch (n->name_len) {
1413
+ case AUDIT_NAME_FULL:
1414
+ /* log the full path */
1415
+ audit_log_format(ab, " name=");
1416
+ audit_log_untrustedstring(ab, n->name->name);
1417
+ break;
1418
+ case 0:
1419
+ /* name was specified as a relative path and the
1420
+ * directory component is the cwd
1421
+ */
1422
+ audit_log_d_path(ab, " name=", &context->pwd);
1423
+ break;
1424
+ default:
1425
+ /* log the name's directory component */
1426
+ audit_log_format(ab, " name=");
1427
+ audit_log_n_untrustedstring(ab, n->name->name,
1428
+ n->name_len);
1429
+ }
1430
+ } else
1431
+ audit_log_format(ab, " name=(null)");
1432
+
1433
+ if (n->ino != AUDIT_INO_UNSET)
1434
+ audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#ho ouid=%u ogid=%u rdev=%02x:%02x",
1435
+ n->ino,
1436
+ MAJOR(n->dev),
1437
+ MINOR(n->dev),
1438
+ n->mode,
1439
+ from_kuid(&init_user_ns, n->uid),
1440
+ from_kgid(&init_user_ns, n->gid),
1441
+ MAJOR(n->rdev),
1442
+ MINOR(n->rdev));
1443
+ if (n->osid != 0) {
1444
+ char *ctx = NULL;
1445
+ u32 len;
1446
+
1447
+ if (security_secid_to_secctx(
1448
+ n->osid, &ctx, &len)) {
1449
+ audit_log_format(ab, " osid=%u", n->osid);
1450
+ if (call_panic)
1451
+ *call_panic = 2;
1452
+ } else {
1453
+ audit_log_format(ab, " obj=%s", ctx);
1454
+ security_release_secctx(ctx, len);
1455
+ }
1456
+ }
1457
+
1458
+ /* log the audit_names record type */
1459
+ switch (n->type) {
1460
+ case AUDIT_TYPE_NORMAL:
1461
+ audit_log_format(ab, " nametype=NORMAL");
1462
+ break;
1463
+ case AUDIT_TYPE_PARENT:
1464
+ audit_log_format(ab, " nametype=PARENT");
1465
+ break;
1466
+ case AUDIT_TYPE_CHILD_DELETE:
1467
+ audit_log_format(ab, " nametype=DELETE");
1468
+ break;
1469
+ case AUDIT_TYPE_CHILD_CREATE:
1470
+ audit_log_format(ab, " nametype=CREATE");
1471
+ break;
1472
+ default:
1473
+ audit_log_format(ab, " nametype=UNKNOWN");
1474
+ break;
1475
+ }
1476
+
1477
+ audit_log_fcaps(ab, n);
1478
+ audit_log_end(ab);
1479
+}
1480
+
1481
+static void audit_log_proctitle(void)
13071482 {
13081483 int res;
13091484 char *buf;
13101485 char *msg = "(null)";
13111486 int len = strlen(msg);
1487
+ struct audit_context *context = audit_context();
13121488 struct audit_buffer *ab;
1489
+
1490
+ if (!context || context->dummy)
1491
+ return;
13131492
13141493 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
13151494 if (!ab)
....@@ -1323,7 +1502,7 @@
13231502 if (!buf)
13241503 goto out;
13251504 /* Historically called this from procfs naming */
1326
- res = get_cmdline(tsk, buf, MAX_PROCTITLE_AUDIT_LEN);
1505
+ res = get_cmdline(current, buf, MAX_PROCTITLE_AUDIT_LEN);
13271506 if (res == 0) {
13281507 kfree(buf);
13291508 goto out;
....@@ -1343,15 +1522,15 @@
13431522 audit_log_end(ab);
13441523 }
13451524
1346
-static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1525
+static void audit_log_exit(void)
13471526 {
13481527 int i, call_panic = 0;
1528
+ struct audit_context *context = audit_context();
13491529 struct audit_buffer *ab;
13501530 struct audit_aux_data *aux;
13511531 struct audit_names *n;
13521532
1353
- /* tsk == current */
1354
- context->personality = tsk->personality;
1533
+ context->personality = current->personality;
13551534
13561535 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
13571536 if (!ab)
....@@ -1373,7 +1552,7 @@
13731552 context->argv[3],
13741553 context->name_count);
13751554
1376
- audit_log_task_info(ab, tsk);
1555
+ audit_log_task_info(ab);
13771556 audit_log_key(ab, context->filterkey);
13781557 audit_log_end(ab);
13791558
....@@ -1399,6 +1578,9 @@
13991578 audit_log_cap(ab, "pi", &axs->new_pcap.inheritable);
14001579 audit_log_cap(ab, "pe", &axs->new_pcap.effective);
14011580 audit_log_cap(ab, "pa", &axs->new_pcap.ambient);
1581
+ audit_log_format(ab, " frootid=%d",
1582
+ from_kuid(&init_user_ns,
1583
+ axs->fcap.rootid));
14021584 break; }
14031585
14041586 }
....@@ -1462,7 +1644,7 @@
14621644 audit_log_name(context, n, NULL, i++, &call_panic);
14631645 }
14641646
1465
- audit_log_proctitle(tsk, context);
1647
+ audit_log_proctitle();
14661648
14671649 /* Send end of event record to help user space know we are finished */
14681650 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
....@@ -1480,22 +1662,31 @@
14801662 */
14811663 void __audit_free(struct task_struct *tsk)
14821664 {
1483
- struct audit_context *context;
1665
+ struct audit_context *context = tsk->audit_context;
14841666
1485
- context = audit_take_context(tsk, 0, 0);
14861667 if (!context)
14871668 return;
14881669
1489
- /* Check for system calls that do not go through the exit
1490
- * function (e.g., exit_group), then free context block.
1491
- * We use GFP_ATOMIC here because we might be doing this
1492
- * in the context of the idle thread */
1493
- /* that can happen only if we are called from do_exit() */
1494
- if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1495
- audit_log_exit(context, tsk);
14961670 if (!list_empty(&context->killed_trees))
1497
- audit_kill_trees(&context->killed_trees);
1671
+ audit_kill_trees(context);
14981672
1673
+ /* We are called either by do_exit() or the fork() error handling code;
1674
+ * in the former case tsk == current and in the latter tsk is a
1675
+ * random task_struct that doesn't doesn't have any meaningful data we
1676
+ * need to log via audit_log_exit().
1677
+ */
1678
+ if (tsk == current && !context->dummy && context->in_syscall) {
1679
+ context->return_valid = 0;
1680
+ context->return_code = 0;
1681
+
1682
+ audit_filter_syscall(tsk, context,
1683
+ &audit_filter_list[AUDIT_FILTER_EXIT]);
1684
+ audit_filter_inodes(tsk, context);
1685
+ if (context->current_state == AUDIT_RECORD_CONTEXT)
1686
+ audit_log_exit();
1687
+ }
1688
+
1689
+ audit_set_context(tsk, NULL);
14991690 audit_free_context(context);
15001691 }
15011692
....@@ -1537,7 +1728,7 @@
15371728 return;
15381729 }
15391730
1540
- context->arch = syscall_get_arch();
1731
+ context->arch = syscall_get_arch(current);
15411732 context->major = major;
15421733 context->argv[0] = a1;
15431734 context->argv[1] = a2;
....@@ -1565,24 +1756,48 @@
15651756 {
15661757 struct audit_context *context;
15671758
1568
- if (success)
1569
- success = AUDITSC_SUCCESS;
1570
- else
1571
- success = AUDITSC_FAILURE;
1572
-
1573
- context = audit_take_context(current, success, return_code);
1759
+ context = audit_context();
15741760 if (!context)
15751761 return;
15761762
1577
- if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1578
- audit_log_exit(context, current);
1763
+ if (!list_empty(&context->killed_trees))
1764
+ audit_kill_trees(context);
1765
+
1766
+ if (!context->dummy && context->in_syscall) {
1767
+ if (success)
1768
+ context->return_valid = AUDITSC_SUCCESS;
1769
+ else
1770
+ context->return_valid = AUDITSC_FAILURE;
1771
+
1772
+ /*
1773
+ * we need to fix up the return code in the audit logs if the
1774
+ * actual return codes are later going to be fixed up by the
1775
+ * arch specific signal handlers
1776
+ *
1777
+ * This is actually a test for:
1778
+ * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
1779
+ * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
1780
+ *
1781
+ * but is faster than a bunch of ||
1782
+ */
1783
+ if (unlikely(return_code <= -ERESTARTSYS) &&
1784
+ (return_code >= -ERESTART_RESTARTBLOCK) &&
1785
+ (return_code != -ENOIOCTLCMD))
1786
+ context->return_code = -EINTR;
1787
+ else
1788
+ context->return_code = return_code;
1789
+
1790
+ audit_filter_syscall(current, context,
1791
+ &audit_filter_list[AUDIT_FILTER_EXIT]);
1792
+ audit_filter_inodes(current, context);
1793
+ if (context->current_state == AUDIT_RECORD_CONTEXT)
1794
+ audit_log_exit();
1795
+ }
15791796
15801797 context->in_syscall = 0;
15811798 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
15821799
1583
- if (!list_empty(&context->killed_trees))
1584
- audit_kill_trees(&context->killed_trees);
1585
-
1800
+ audit_free_module(context);
15861801 audit_free_names(context);
15871802 unroll_tree_refs(context, NULL, 0);
15881803 audit_free_aux(context);
....@@ -1597,12 +1812,10 @@
15971812 kfree(context->filterkey);
15981813 context->filterkey = NULL;
15991814 }
1600
- audit_set_context(current, context);
16011815 }
16021816
16031817 static inline void handle_one(const struct inode *inode)
16041818 {
1605
-#ifdef CONFIG_AUDIT_TREE
16061819 struct audit_context *context;
16071820 struct audit_tree_refs *p;
16081821 struct audit_chunk *chunk;
....@@ -1627,12 +1840,10 @@
16271840 return;
16281841 }
16291842 put_tree_ref(context, chunk);
1630
-#endif
16311843 }
16321844
16331845 static void handle_path(const struct dentry *dentry)
16341846 {
1635
-#ifdef CONFIG_AUDIT_TREE
16361847 struct audit_context *context;
16371848 struct audit_tree_refs *p;
16381849 const struct dentry *d, *parent;
....@@ -1685,7 +1896,6 @@
16851896 return;
16861897 }
16871898 rcu_read_unlock();
1688
-#endif
16891899 }
16901900
16911901 static struct audit_names *audit_alloc_name(struct audit_context *context,
....@@ -1736,6 +1946,20 @@
17361946 return NULL;
17371947 }
17381948
1949
+inline void _audit_getcwd(struct audit_context *context)
1950
+{
1951
+ if (!context->pwd.dentry)
1952
+ get_fs_pwd(current->fs, &context->pwd);
1953
+}
1954
+
1955
+void __audit_getcwd(void)
1956
+{
1957
+ struct audit_context *context = audit_context();
1958
+
1959
+ if (context->in_syscall)
1960
+ _audit_getcwd(context);
1961
+}
1962
+
17391963 /**
17401964 * __audit_getname - add a name to the list
17411965 * @name: name to add
....@@ -1760,8 +1984,49 @@
17601984 name->aname = n;
17611985 name->refcnt++;
17621986
1763
- if (!context->pwd.dentry)
1764
- get_fs_pwd(current->fs, &context->pwd);
1987
+ _audit_getcwd(context);
1988
+}
1989
+
1990
+static inline int audit_copy_fcaps(struct audit_names *name,
1991
+ const struct dentry *dentry)
1992
+{
1993
+ struct cpu_vfs_cap_data caps;
1994
+ int rc;
1995
+
1996
+ if (!dentry)
1997
+ return 0;
1998
+
1999
+ rc = get_vfs_caps_from_disk(dentry, &caps);
2000
+ if (rc)
2001
+ return rc;
2002
+
2003
+ name->fcap.permitted = caps.permitted;
2004
+ name->fcap.inheritable = caps.inheritable;
2005
+ name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2006
+ name->fcap.rootid = caps.rootid;
2007
+ name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
2008
+ VFS_CAP_REVISION_SHIFT;
2009
+
2010
+ return 0;
2011
+}
2012
+
2013
+/* Copy inode data into an audit_names. */
2014
+static void audit_copy_inode(struct audit_names *name,
2015
+ const struct dentry *dentry,
2016
+ struct inode *inode, unsigned int flags)
2017
+{
2018
+ name->ino = inode->i_ino;
2019
+ name->dev = inode->i_sb->s_dev;
2020
+ name->mode = inode->i_mode;
2021
+ name->uid = inode->i_uid;
2022
+ name->gid = inode->i_gid;
2023
+ name->rdev = inode->i_rdev;
2024
+ security_inode_getsecid(inode, &name->osid);
2025
+ if (flags & AUDIT_INODE_NOEVAL) {
2026
+ name->fcap_ver = -1;
2027
+ return;
2028
+ }
2029
+ audit_copy_fcaps(name, dentry);
17652030 }
17662031
17672032 /**
....@@ -1777,9 +2042,28 @@
17772042 struct inode *inode = d_backing_inode(dentry);
17782043 struct audit_names *n;
17792044 bool parent = flags & AUDIT_INODE_PARENT;
2045
+ struct audit_entry *e;
2046
+ struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
2047
+ int i;
17802048
17812049 if (!context->in_syscall)
17822050 return;
2051
+
2052
+ rcu_read_lock();
2053
+ list_for_each_entry_rcu(e, list, list) {
2054
+ for (i = 0; i < e->rule.field_count; i++) {
2055
+ struct audit_field *f = &e->rule.fields[i];
2056
+
2057
+ if (f->type == AUDIT_FSTYPE
2058
+ && audit_comparator(inode->i_sb->s_magic,
2059
+ f->op, f->val)
2060
+ && e->rule.action == AUDIT_NEVER) {
2061
+ rcu_read_unlock();
2062
+ return;
2063
+ }
2064
+ }
2065
+ }
2066
+ rcu_read_unlock();
17832067
17842068 if (!name)
17852069 goto out_alloc;
....@@ -1846,7 +2130,7 @@
18462130 n->type = AUDIT_TYPE_NORMAL;
18472131 }
18482132 handle_path(dentry);
1849
- audit_copy_inode(n, dentry, inode);
2133
+ audit_copy_inode(n, dentry, inode, flags & AUDIT_INODE_NOEVAL);
18502134 }
18512135
18522136 void __audit_file(const struct file *file)
....@@ -1874,7 +2158,7 @@
18742158 {
18752159 struct audit_context *context = audit_context();
18762160 struct inode *inode = d_backing_inode(dentry);
1877
- const char *dname = dentry->d_name.name;
2161
+ const struct qstr *dname = &dentry->d_name;
18782162 struct audit_names *n, *found_parent = NULL, *found_child = NULL;
18792163 struct audit_entry *e;
18802164 struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
....@@ -1884,20 +2168,16 @@
18842168 return;
18852169
18862170 rcu_read_lock();
1887
- if (!list_empty(list)) {
1888
- list_for_each_entry_rcu(e, list, list) {
1889
- for (i = 0; i < e->rule.field_count; i++) {
1890
- struct audit_field *f = &e->rule.fields[i];
2171
+ list_for_each_entry_rcu(e, list, list) {
2172
+ for (i = 0; i < e->rule.field_count; i++) {
2173
+ struct audit_field *f = &e->rule.fields[i];
18912174
1892
- if (f->type == AUDIT_FSTYPE) {
1893
- if (audit_comparator(parent->i_sb->s_magic,
1894
- f->op, f->val)) {
1895
- if (e->rule.action == AUDIT_NEVER) {
1896
- rcu_read_unlock();
1897
- return;
1898
- }
1899
- }
1900
- }
2175
+ if (f->type == AUDIT_FSTYPE
2176
+ && audit_comparator(parent->i_sb->s_magic,
2177
+ f->op, f->val)
2178
+ && e->rule.action == AUDIT_NEVER) {
2179
+ rcu_read_unlock();
2180
+ return;
19012181 }
19022182 }
19032183 }
....@@ -1923,6 +2203,8 @@
19232203 }
19242204 }
19252205
2206
+ cond_resched();
2207
+
19262208 /* is there a matching child entry? */
19272209 list_for_each_entry(n, &context->names_list, list) {
19282210 /* can only match entries that have a name */
....@@ -1930,7 +2212,7 @@
19302212 (n->type != type && n->type != AUDIT_TYPE_UNKNOWN))
19312213 continue;
19322214
1933
- if (!strcmp(dname, n->name->name) ||
2215
+ if (!strcmp(dname->name, n->name->name) ||
19342216 !audit_compare_dname_path(dname, n->name->name,
19352217 found_parent ?
19362218 found_parent->name_len :
....@@ -1947,7 +2229,7 @@
19472229 n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
19482230 if (!n)
19492231 return;
1950
- audit_copy_inode(n, NULL, parent);
2232
+ audit_copy_inode(n, NULL, parent, 0);
19512233 }
19522234
19532235 if (!found_child) {
....@@ -1966,7 +2248,7 @@
19662248 }
19672249
19682250 if (inode)
1969
- audit_copy_inode(found_child, dentry, inode);
2251
+ audit_copy_inode(found_child, dentry, inode, 0);
19702252 else
19712253 found_child->ino = AUDIT_INO_UNSET;
19722254 }
....@@ -1995,91 +2277,6 @@
19952277 ctx->current_state = AUDIT_RECORD_CONTEXT;
19962278 }
19972279 return 1;
1998
-}
1999
-
2000
-/* global counter which is incremented every time something logs in */
2001
-static atomic_t session_id = ATOMIC_INIT(0);
2002
-
2003
-static int audit_set_loginuid_perm(kuid_t loginuid)
2004
-{
2005
- /* if we are unset, we don't need privs */
2006
- if (!audit_loginuid_set(current))
2007
- return 0;
2008
- /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
2009
- if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
2010
- return -EPERM;
2011
- /* it is set, you need permission */
2012
- if (!capable(CAP_AUDIT_CONTROL))
2013
- return -EPERM;
2014
- /* reject if this is not an unset and we don't allow that */
2015
- if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid))
2016
- return -EPERM;
2017
- return 0;
2018
-}
2019
-
2020
-static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
2021
- unsigned int oldsessionid, unsigned int sessionid,
2022
- int rc)
2023
-{
2024
- struct audit_buffer *ab;
2025
- uid_t uid, oldloginuid, loginuid;
2026
- struct tty_struct *tty;
2027
-
2028
- if (!audit_enabled)
2029
- return;
2030
-
2031
- ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2032
- if (!ab)
2033
- return;
2034
-
2035
- uid = from_kuid(&init_user_ns, task_uid(current));
2036
- oldloginuid = from_kuid(&init_user_ns, koldloginuid);
2037
- loginuid = from_kuid(&init_user_ns, kloginuid),
2038
- tty = audit_get_tty(current);
2039
-
2040
- audit_log_format(ab, "pid=%d uid=%u", task_tgid_nr(current), uid);
2041
- audit_log_task_context(ab);
2042
- audit_log_format(ab, " old-auid=%u auid=%u tty=%s old-ses=%u ses=%u res=%d",
2043
- oldloginuid, loginuid, tty ? tty_name(tty) : "(none)",
2044
- oldsessionid, sessionid, !rc);
2045
- audit_put_tty(tty);
2046
- audit_log_end(ab);
2047
-}
2048
-
2049
-/**
2050
- * audit_set_loginuid - set current task's audit_context loginuid
2051
- * @loginuid: loginuid value
2052
- *
2053
- * Returns 0.
2054
- *
2055
- * Called (set) from fs/proc/base.c::proc_loginuid_write().
2056
- */
2057
-int audit_set_loginuid(kuid_t loginuid)
2058
-{
2059
- struct task_struct *task = current;
2060
- unsigned int oldsessionid, sessionid = AUDIT_SID_UNSET;
2061
- kuid_t oldloginuid;
2062
- int rc;
2063
-
2064
- oldloginuid = audit_get_loginuid(current);
2065
- oldsessionid = audit_get_sessionid(current);
2066
-
2067
- rc = audit_set_loginuid_perm(loginuid);
2068
- if (rc)
2069
- goto out;
2070
-
2071
- /* are we setting or clearing? */
2072
- if (uid_valid(loginuid)) {
2073
- sessionid = (unsigned int)atomic_inc_return(&session_id);
2074
- if (unlikely(sessionid == AUDIT_SID_UNSET))
2075
- sessionid = (unsigned int)atomic_inc_return(&session_id);
2076
- }
2077
-
2078
- task->sessionid = sessionid;
2079
- task->loginuid = loginuid;
2080
-out:
2081
- audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
2082
- return rc;
20832280 }
20842281
20852282 /**
....@@ -2276,30 +2473,17 @@
22762473 }
22772474
22782475 /**
2279
- * audit_signal_info - record signal info for shutting down audit subsystem
2280
- * @sig: signal value
2476
+ * audit_signal_info_syscall - record signal info for syscalls
22812477 * @t: task being signaled
22822478 *
22832479 * If the audit subsystem is being terminated, record the task (pid)
22842480 * and uid that is doing that.
22852481 */
2286
-int audit_signal_info(int sig, struct task_struct *t)
2482
+int audit_signal_info_syscall(struct task_struct *t)
22872483 {
22882484 struct audit_aux_data_pids *axp;
22892485 struct audit_context *ctx = audit_context();
2290
- kuid_t uid = current_uid(), auid, t_uid = task_uid(t);
2291
-
2292
- if (auditd_test_task(t) &&
2293
- (sig == SIGTERM || sig == SIGHUP ||
2294
- sig == SIGUSR1 || sig == SIGUSR2)) {
2295
- audit_sig_pid = task_tgid_nr(current);
2296
- auid = audit_get_loginuid(current);
2297
- if (uid_valid(auid))
2298
- audit_sig_uid = auid;
2299
- else
2300
- audit_sig_uid = uid;
2301
- security_task_getsecid(current, &audit_sig_sid);
2302
- }
2486
+ kuid_t t_uid = task_uid(t);
23032487
23042488 if (!audit_signals || audit_dummy_context())
23052489 return 0;
....@@ -2370,6 +2554,7 @@
23702554 ax->fcap.permitted = vcaps.permitted;
23712555 ax->fcap.inheritable = vcaps.inheritable;
23722556 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2557
+ ax->fcap.rootid = vcaps.rootid;
23732558 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
23742559
23752560 ax->old_pcap.permitted = old->cap_permitted;
....@@ -2426,6 +2611,50 @@
24262611 audit_log(audit_context(), GFP_KERNEL,
24272612 AUDIT_FANOTIFY, "resp=%u", response);
24282613 }
2614
+
2615
+void __audit_tk_injoffset(struct timespec64 offset)
2616
+{
2617
+ struct audit_context *context = audit_context();
2618
+
2619
+ /* only set type if not already set by NTP */
2620
+ if (!context->type)
2621
+ context->type = AUDIT_TIME_INJOFFSET;
2622
+ memcpy(&context->time.tk_injoffset, &offset, sizeof(offset));
2623
+}
2624
+
2625
+void __audit_ntp_log(const struct audit_ntp_data *ad)
2626
+{
2627
+ struct audit_context *context = audit_context();
2628
+ int type;
2629
+
2630
+ for (type = 0; type < AUDIT_NTP_NVALS; type++)
2631
+ if (ad->vals[type].newval != ad->vals[type].oldval) {
2632
+ /* unconditionally set type, overwriting TK */
2633
+ context->type = AUDIT_TIME_ADJNTPVAL;
2634
+ memcpy(&context->time.ntp_data, ad, sizeof(*ad));
2635
+ break;
2636
+ }
2637
+}
2638
+
2639
+void __audit_log_nfcfg(const char *name, u8 af, unsigned int nentries,
2640
+ enum audit_nfcfgop op, gfp_t gfp)
2641
+{
2642
+ struct audit_buffer *ab;
2643
+ char comm[sizeof(current->comm)];
2644
+
2645
+ ab = audit_log_start(audit_context(), gfp, AUDIT_NETFILTER_CFG);
2646
+ if (!ab)
2647
+ return;
2648
+ audit_log_format(ab, "table=%s family=%u entries=%u op=%s",
2649
+ name, af, nentries, audit_nfcfgs[op].s);
2650
+
2651
+ audit_log_format(ab, " pid=%u", task_pid_nr(current));
2652
+ audit_log_task_context(ab); /* subj= */
2653
+ audit_log_format(ab, " comm=");
2654
+ audit_log_untrustedstring(ab, get_task_comm(comm, current));
2655
+ audit_log_end(ab);
2656
+}
2657
+EXPORT_SYMBOL_GPL(__audit_log_nfcfg);
24292658
24302659 static void audit_log_task(struct audit_buffer *ab)
24312660 {
....@@ -2495,7 +2724,7 @@
24952724 return;
24962725 audit_log_task(ab);
24972726 audit_log_format(ab, " sig=%ld arch=%x syscall=%ld compat=%d ip=0x%lx code=0x%x",
2498
- signr, syscall_get_arch(), syscall,
2727
+ signr, syscall_get_arch(current), syscall,
24992728 in_compat_syscall(), KSTK_EIP(current), code);
25002729 audit_log_end(ab);
25012730 }
....@@ -2513,10 +2742,9 @@
25132742 if (unlikely(!ab))
25142743 return;
25152744
2516
- audit_log_format(ab, "op=seccomp-logging");
2517
- audit_log_format(ab, " actions=%s", names);
2518
- audit_log_format(ab, " old-actions=%s", old_names);
2519
- audit_log_format(ab, " res=%d", res);
2745
+ audit_log_format(ab,
2746
+ "op=seccomp-logging actions=%s old-actions=%s res=%d",
2747
+ names, old_names, res);
25202748 audit_log_end(ab);
25212749 }
25222750