Skip to content

Commit b24a30a

Browse files
committed
audit: fix event coverage of AUDIT_ANOM_LINK
The userspace audit tools didn't like the existing formatting of the AUDIT_ANOM_LINK event. It needed to be expanded to emit an AUDIT_PATH event as well, so this implements the change. The bulk of the patch is moving code out of auditsc.c into audit.c and audit.h for general use. It expands audit_log_name to include an optional "struct path" argument for the simple case of just needing to report a pathname. This also makes audit_log_task_info available when syscall auditing is not enabled, since it is needed in either case for process details. Signed-off-by: Kees Cook <keescook@chromium.org> Reported-by: Steve Grubb <sgrubb@redhat.com>
1 parent 7173c54 commit b24a30a

4 files changed

Lines changed: 405 additions & 369 deletions

File tree

‎include/linux/audit.h‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ static inline int audit_get_sessionid(struct task_struct *tsk)
190190
return tsk->sessionid;
191191
}
192192

193-
extern int audit_log_task_context(struct audit_buffer *ab);
194-
extern void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk);
195193
extern void __audit_ipc_obj(struct kern_ipc_perm *ipcp);
196194
extern void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode);
197195
extern int __audit_bprm(struct linux_binprm *bprm);
@@ -346,13 +344,6 @@ static inline int audit_get_sessionid(struct task_struct *tsk)
346344
{
347345
return -1;
348346
}
349-
static int void audit_log_task_context(struct audit_buffer *ab)
350-
{
351-
return 0;
352-
}
353-
static inline void audit_log_task_info(struct audit_buffer *ab,
354-
struct task_struct *tsk)
355-
{ }
356347
static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
357348
{ }
358349
static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
@@ -439,6 +430,10 @@ static inline void audit_log_secctx(struct audit_buffer *ab, u32 secid)
439430
{ }
440431
#endif
441432

433+
extern int audit_log_task_context(struct audit_buffer *ab);
434+
extern void audit_log_task_info(struct audit_buffer *ab,
435+
struct task_struct *tsk);
436+
442437
extern int audit_update_lsm_rules(void);
443438

444439
/* Private API (for audit.c only) */
@@ -485,6 +480,13 @@ static inline void audit_log_link_denied(const char *string,
485480
{ }
486481
static inline void audit_log_secctx(struct audit_buffer *ab, u32 secid)
487482
{ }
483+
static inline int audit_log_task_context(struct audit_buffer *ab)
484+
{
485+
return 0;
486+
}
487+
static inline void audit_log_task_info(struct audit_buffer *ab,
488+
struct task_struct *tsk)
489+
{ }
488490
#define audit_enabled 0
489491
#endif /* CONFIG_AUDIT */
490492
static inline void audit_log_string(struct audit_buffer *ab, const char *buf)

‎kernel/audit.c‎

Lines changed: 236 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <linux/err.h>
5151
#include <linux/kthread.h>
5252
#include <linux/kernel.h>
53+
#include <linux/syscalls.h>
5354

5455
#include <linux/audit.h>
5556

@@ -1393,6 +1394,224 @@ void audit_log_key(struct audit_buffer *ab, char *key)
13931394
audit_log_format(ab, "(null)");
13941395
}
13951396

1397+
void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1398+
{
1399+
int i;
1400+
1401+
audit_log_format(ab, " %s=", prefix);
1402+
CAP_FOR_EACH_U32(i) {
1403+
audit_log_format(ab, "%08x",
1404+
cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1405+
}
1406+
}
1407+
1408+
void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1409+
{
1410+
kernel_cap_t *perm = &name->fcap.permitted;
1411+
kernel_cap_t *inh = &name->fcap.inheritable;
1412+
int log = 0;
1413+
1414+
if (!cap_isclear(*perm)) {
1415+
audit_log_cap(ab, "cap_fp", perm);
1416+
log = 1;
1417+
}
1418+
if (!cap_isclear(*inh)) {
1419+
audit_log_cap(ab, "cap_fi", inh);
1420+
log = 1;
1421+
}
1422+
1423+
if (log)
1424+
audit_log_format(ab, " cap_fe=%d cap_fver=%x",
1425+
name->fcap.fE, name->fcap_ver);
1426+
}
1427+
1428+
static inline int audit_copy_fcaps(struct audit_names *name,
1429+
const struct dentry *dentry)
1430+
{
1431+
struct cpu_vfs_cap_data caps;
1432+
int rc;
1433+
1434+
if (!dentry)
1435+
return 0;
1436+
1437+
rc = get_vfs_caps_from_disk(dentry, &caps);
1438+
if (rc)
1439+
return rc;
1440+
1441+
name->fcap.permitted = caps.permitted;
1442+
name->fcap.inheritable = caps.inheritable;
1443+
name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1444+
name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >>
1445+
VFS_CAP_REVISION_SHIFT;
1446+
1447+
return 0;
1448+
}
1449+
1450+
/* Copy inode data into an audit_names. */
1451+
void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1452+
const struct inode *inode)
1453+
{
1454+
name->ino = inode->i_ino;
1455+
name->dev = inode->i_sb->s_dev;
1456+
name->mode = inode->i_mode;
1457+
name->uid = inode->i_uid;
1458+
name->gid = inode->i_gid;
1459+
name->rdev = inode->i_rdev;
1460+
security_inode_getsecid(inode, &name->osid);
1461+
audit_copy_fcaps(name, dentry);
1462+
}
1463+
1464+
/**
1465+
* audit_log_name - produce AUDIT_PATH record from struct audit_names
1466+
* @context: audit_context for the task
1467+
* @n: audit_names structure with reportable details
1468+
* @path: optional path to report instead of audit_names->name
1469+
* @record_num: record number to report when handling a list of names
1470+
* @call_panic: optional pointer to int that will be updated if secid fails
1471+
*/
1472+
void audit_log_name(struct audit_context *context, struct audit_names *n,
1473+
struct path *path, int record_num, int *call_panic)
1474+
{
1475+
struct audit_buffer *ab;
1476+
ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1477+
if (!ab)
1478+
return;
1479+
1480+
audit_log_format(ab, "item=%d", record_num);
1481+
1482+
if (path)
1483+
audit_log_d_path(ab, " name=", path);
1484+
else if (n->name) {
1485+
switch (n->name_len) {
1486+
case AUDIT_NAME_FULL:
1487+
/* log the full path */
1488+
audit_log_format(ab, " name=");
1489+
audit_log_untrustedstring(ab, n->name->name);
1490+
break;
1491+
case 0:
1492+
/* name was specified as a relative path and the
1493+
* directory component is the cwd */
1494+
audit_log_d_path(ab, " name=", &context->pwd);
1495+
break;
1496+
default:
1497+
/* log the name's directory component */
1498+
audit_log_format(ab, " name=");
1499+
audit_log_n_untrustedstring(ab, n->name->name,
1500+
n->name_len);
1501+
}
1502+
} else
1503+
audit_log_format(ab, " name=(null)");
1504+
1505+
if (n->ino != (unsigned long)-1) {
1506+
audit_log_format(ab, " inode=%lu"
1507+
" dev=%02x:%02x mode=%#ho"
1508+
" ouid=%u ogid=%u rdev=%02x:%02x",
1509+
n->ino,
1510+
MAJOR(n->dev),
1511+
MINOR(n->dev),
1512+
n->mode,
1513+
from_kuid(&init_user_ns, n->uid),
1514+
from_kgid(&init_user_ns, n->gid),
1515+
MAJOR(n->rdev),
1516+
MINOR(n->rdev));
1517+
}
1518+
if (n->osid != 0) {
1519+
char *ctx = NULL;
1520+
u32 len;
1521+
if (security_secid_to_secctx(
1522+
n->osid, &ctx, &len)) {
1523+
audit_log_format(ab, " osid=%u", n->osid);
1524+
if (call_panic)
1525+
*call_panic = 2;
1526+
} else {
1527+
audit_log_format(ab, " obj=%s", ctx);
1528+
security_release_secctx(ctx, len);
1529+
}
1530+
}
1531+
1532+
audit_log_fcaps(ab, n);
1533+
audit_log_end(ab);
1534+
}
1535+
1536+
int audit_log_task_context(struct audit_buffer *ab)
1537+
{
1538+
char *ctx = NULL;
1539+
unsigned len;
1540+
int error;
1541+
u32 sid;
1542+
1543+
security_task_getsecid(current, &sid);
1544+
if (!sid)
1545+
return 0;
1546+
1547+
error = security_secid_to_secctx(sid, &ctx, &len);
1548+
if (error) {
1549+
if (error != -EINVAL)
1550+
goto error_path;
1551+
return 0;
1552+
}
1553+
1554+
audit_log_format(ab, " subj=%s", ctx);
1555+
security_release_secctx(ctx, len);
1556+
return 0;
1557+
1558+
error_path:
1559+
audit_panic("error in audit_log_task_context");
1560+
return error;
1561+
}
1562+
EXPORT_SYMBOL(audit_log_task_context);
1563+
1564+
void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
1565+
{
1566+
const struct cred *cred;
1567+
char name[sizeof(tsk->comm)];
1568+
struct mm_struct *mm = tsk->mm;
1569+
char *tty;
1570+
1571+
if (!ab)
1572+
return;
1573+
1574+
/* tsk == current */
1575+
cred = current_cred();
1576+
1577+
spin_lock_irq(&tsk->sighand->siglock);
1578+
if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1579+
tty = tsk->signal->tty->name;
1580+
else
1581+
tty = "(none)";
1582+
spin_unlock_irq(&tsk->sighand->siglock);
1583+
1584+
audit_log_format(ab,
1585+
" ppid=%ld pid=%d auid=%u uid=%u gid=%u"
1586+
" euid=%u suid=%u fsuid=%u"
1587+
" egid=%u sgid=%u fsgid=%u ses=%u tty=%s",
1588+
sys_getppid(),
1589+
tsk->pid,
1590+
from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
1591+
from_kuid(&init_user_ns, cred->uid),
1592+
from_kgid(&init_user_ns, cred->gid),
1593+
from_kuid(&init_user_ns, cred->euid),
1594+
from_kuid(&init_user_ns, cred->suid),
1595+
from_kuid(&init_user_ns, cred->fsuid),
1596+
from_kgid(&init_user_ns, cred->egid),
1597+
from_kgid(&init_user_ns, cred->sgid),
1598+
from_kgid(&init_user_ns, cred->fsgid),
1599+
audit_get_sessionid(tsk), tty);
1600+
1601+
get_task_comm(name, tsk);
1602+
audit_log_format(ab, " comm=");
1603+
audit_log_untrustedstring(ab, name);
1604+
1605+
if (mm) {
1606+
down_read(&mm->mmap_sem);
1607+
if (mm->exe_file)
1608+
audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
1609+
up_read(&mm->mmap_sem);
1610+
}
1611+
audit_log_task_context(ab);
1612+
}
1613+
EXPORT_SYMBOL(audit_log_task_info);
1614+
13961615
/**
13971616
* audit_log_link_denied - report a link restriction denial
13981617
* @operation: specific link opreation
@@ -1401,19 +1620,28 @@ void audit_log_key(struct audit_buffer *ab, char *key)
14011620
void audit_log_link_denied(const char *operation, struct path *link)
14021621
{
14031622
struct audit_buffer *ab;
1623+
struct audit_names *name;
1624+
1625+
name = kzalloc(sizeof(*name), GFP_NOFS);
1626+
if (!name)
1627+
return;
14041628

1629+
/* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */
14051630
ab = audit_log_start(current->audit_context, GFP_KERNEL,
14061631
AUDIT_ANOM_LINK);
14071632
if (!ab)
1408-
return;
1409-
audit_log_format(ab, "op=%s action=denied", operation);
1410-
audit_log_format(ab, " pid=%d comm=", current->pid);
1411-
audit_log_untrustedstring(ab, current->comm);
1412-
audit_log_d_path(ab, " path=", link);
1413-
audit_log_format(ab, " dev=");
1414-
audit_log_untrustedstring(ab, link->dentry->d_inode->i_sb->s_id);
1415-
audit_log_format(ab, " ino=%lu", link->dentry->d_inode->i_ino);
1633+
goto out;
1634+
audit_log_format(ab, "op=%s", operation);
1635+
audit_log_task_info(ab, current);
1636+
audit_log_format(ab, " res=0");
14161637
audit_log_end(ab);
1638+
1639+
/* Generate AUDIT_PATH record with object. */
1640+
name->type = AUDIT_TYPE_NORMAL;
1641+
audit_copy_inode(name, link->dentry, link->dentry->d_inode);
1642+
audit_log_name(current->audit_context, name, link, 0, NULL);
1643+
out:
1644+
kfree(name);
14171645
}
14181646

14191647
/**

0 commit comments

Comments
 (0)