Skip to content

Commit ca57ec0

Browse files
committed
audit: Add typespecific uid and gid comparators
The audit filter code guarantees that uid are always compared with uids and gids are always compared with gids, as the comparason operations are type specific. Take advantage of this proper to define audit_uid_comparator and audit_gid_comparator which use the type safe comparasons from uidgid.h. Build on audit_uid_comparator and audit_gid_comparator and replace audit_compare_id with audit_compare_uid and audit_compare_gid. This is one of those odd cases where being type safe and duplicating code leads to simpler shorter and more concise code. Don't allow bitmask operations in uid and gid comparisons in audit_data_to_entry. Bitmask operations are already denined in audit_rule_to_entry. Convert constants in audit_rule_to_entry and audit_data_to_entry into kuids and kgids when appropriate. Convert the uid and gid field in struct audit_names to be of type kuid_t and kgid_t respectively, so that the new uid and gid comparators can be applied in a type safe manner. Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Eric Paris <eparis@redhat.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent 860c0aa commit ca57ec0

4 files changed

Lines changed: 184 additions & 89 deletions

File tree

‎include/linux/audit.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ struct audit_krule {
442442
struct audit_field {
443443
u32 type;
444444
u32 val;
445+
kuid_t uid;
446+
kgid_t gid;
445447
u32 op;
446448
char *lsm_str;
447449
void *lsm_rule;

‎kernel/audit.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ static inline int audit_hash_ino(u32 ino)
7676

7777
extern int audit_match_class(int class, unsigned syscall);
7878
extern int audit_comparator(const u32 left, const u32 op, const u32 right);
79+
extern int audit_uid_comparator(kuid_t left, u32 op, kuid_t right);
80+
extern int audit_gid_comparator(kgid_t left, u32 op, kgid_t right);
7981
extern int audit_compare_dname_path(const char *dname, const char *path,
8082
int *dirlen);
8183
extern struct sk_buff * audit_make_reply(int pid, int seq, int type,

‎kernel/auditfilter.c‎

Lines changed: 109 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
342342

343343
f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
344344
f->val = rule->values[i];
345+
f->uid = INVALID_UID;
346+
f->gid = INVALID_GID;
345347

346348
err = -EINVAL;
347349
if (f->op == Audit_bad)
@@ -350,16 +352,32 @@ static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
350352
switch(f->type) {
351353
default:
352354
goto exit_free;
353-
case AUDIT_PID:
354355
case AUDIT_UID:
355356
case AUDIT_EUID:
356357
case AUDIT_SUID:
357358
case AUDIT_FSUID:
359+
case AUDIT_LOGINUID:
360+
/* bit ops not implemented for uid comparisons */
361+
if (f->op == Audit_bitmask || f->op == Audit_bittest)
362+
goto exit_free;
363+
364+
f->uid = make_kuid(current_user_ns(), f->val);
365+
if (!uid_valid(f->uid))
366+
goto exit_free;
367+
break;
358368
case AUDIT_GID:
359369
case AUDIT_EGID:
360370
case AUDIT_SGID:
361371
case AUDIT_FSGID:
362-
case AUDIT_LOGINUID:
372+
/* bit ops not implemented for gid comparisons */
373+
if (f->op == Audit_bitmask || f->op == Audit_bittest)
374+
goto exit_free;
375+
376+
f->gid = make_kgid(current_user_ns(), f->val);
377+
if (!gid_valid(f->gid))
378+
goto exit_free;
379+
break;
380+
case AUDIT_PID:
363381
case AUDIT_PERS:
364382
case AUDIT_MSGTYPE:
365383
case AUDIT_PPID:
@@ -437,19 +455,39 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
437455

438456
f->type = data->fields[i];
439457
f->val = data->values[i];
458+
f->uid = INVALID_UID;
459+
f->gid = INVALID_GID;
440460
f->lsm_str = NULL;
441461
f->lsm_rule = NULL;
442462
switch(f->type) {
443-
case AUDIT_PID:
444463
case AUDIT_UID:
445464
case AUDIT_EUID:
446465
case AUDIT_SUID:
447466
case AUDIT_FSUID:
467+
case AUDIT_LOGINUID:
468+
case AUDIT_OBJ_UID:
469+
/* bit ops not implemented for uid comparisons */
470+
if (f->op == Audit_bitmask || f->op == Audit_bittest)
471+
goto exit_free;
472+
473+
f->uid = make_kuid(current_user_ns(), f->val);
474+
if (!uid_valid(f->uid))
475+
goto exit_free;
476+
break;
448477
case AUDIT_GID:
449478
case AUDIT_EGID:
450479
case AUDIT_SGID:
451480
case AUDIT_FSGID:
452-
case AUDIT_LOGINUID:
481+
case AUDIT_OBJ_GID:
482+
/* bit ops not implemented for gid comparisons */
483+
if (f->op == Audit_bitmask || f->op == Audit_bittest)
484+
goto exit_free;
485+
486+
f->gid = make_kgid(current_user_ns(), f->val);
487+
if (!gid_valid(f->gid))
488+
goto exit_free;
489+
break;
490+
case AUDIT_PID:
453491
case AUDIT_PERS:
454492
case AUDIT_MSGTYPE:
455493
case AUDIT_PPID:
@@ -461,8 +499,6 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
461499
case AUDIT_ARG1:
462500
case AUDIT_ARG2:
463501
case AUDIT_ARG3:
464-
case AUDIT_OBJ_UID:
465-
case AUDIT_OBJ_GID:
466502
break;
467503
case AUDIT_ARCH:
468504
entry->rule.arch_f = f;
@@ -707,6 +743,23 @@ static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
707743
if (strcmp(a->filterkey, b->filterkey))
708744
return 1;
709745
break;
746+
case AUDIT_UID:
747+
case AUDIT_EUID:
748+
case AUDIT_SUID:
749+
case AUDIT_FSUID:
750+
case AUDIT_LOGINUID:
751+
case AUDIT_OBJ_UID:
752+
if (!uid_eq(a->fields[i].uid, b->fields[i].uid))
753+
return 1;
754+
break;
755+
case AUDIT_GID:
756+
case AUDIT_EGID:
757+
case AUDIT_SGID:
758+
case AUDIT_FSGID:
759+
case AUDIT_OBJ_GID:
760+
if (!gid_eq(a->fields[i].gid, b->fields[i].gid))
761+
return 1;
762+
break;
710763
default:
711764
if (a->fields[i].val != b->fields[i].val)
712765
return 1;
@@ -1198,6 +1251,52 @@ int audit_comparator(u32 left, u32 op, u32 right)
11981251
}
11991252
}
12001253

1254+
int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
1255+
{
1256+
switch (op) {
1257+
case Audit_equal:
1258+
return uid_eq(left, right);
1259+
case Audit_not_equal:
1260+
return !uid_eq(left, right);
1261+
case Audit_lt:
1262+
return uid_lt(left, right);
1263+
case Audit_le:
1264+
return uid_lte(left, right);
1265+
case Audit_gt:
1266+
return uid_gt(left, right);
1267+
case Audit_ge:
1268+
return uid_gte(left, right);
1269+
case Audit_bitmask:
1270+
case Audit_bittest:
1271+
default:
1272+
BUG();
1273+
return 0;
1274+
}
1275+
}
1276+
1277+
int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
1278+
{
1279+
switch (op) {
1280+
case Audit_equal:
1281+
return gid_eq(left, right);
1282+
case Audit_not_equal:
1283+
return !gid_eq(left, right);
1284+
case Audit_lt:
1285+
return gid_lt(left, right);
1286+
case Audit_le:
1287+
return gid_lte(left, right);
1288+
case Audit_gt:
1289+
return gid_gt(left, right);
1290+
case Audit_ge:
1291+
return gid_gte(left, right);
1292+
case Audit_bitmask:
1293+
case Audit_bittest:
1294+
default:
1295+
BUG();
1296+
return 0;
1297+
}
1298+
}
1299+
12011300
/* Compare given dentry name with last component in given path,
12021301
* return of 0 indicates a match. */
12031302
int audit_compare_dname_path(const char *dname, const char *path,
@@ -1251,14 +1350,14 @@ static int audit_filter_user_rules(struct audit_krule *rule,
12511350
result = audit_comparator(task_pid_vnr(current), f->op, f->val);
12521351
break;
12531352
case AUDIT_UID:
1254-
result = audit_comparator(current_uid(), f->op, f->val);
1353+
result = audit_uid_comparator(current_uid(), f->op, f->uid);
12551354
break;
12561355
case AUDIT_GID:
1257-
result = audit_comparator(current_gid(), f->op, f->val);
1356+
result = audit_gid_comparator(current_gid(), f->op, f->gid);
12581357
break;
12591358
case AUDIT_LOGINUID:
1260-
result = audit_comparator(audit_get_loginuid(current),
1261-
f->op, f->val);
1359+
result = audit_uid_comparator(audit_get_loginuid(current),
1360+
f->op, f->uid);
12621361
break;
12631362
case AUDIT_SUBJ_USER:
12641363
case AUDIT_SUBJ_ROLE:

0 commit comments

Comments
 (0)