Skip to content

Commit 9c937dc

Browse files
Amy GriffisAl Viro
authored andcommitted
[PATCH] log more info for directory entry change events
When an audit event involves changes to a directory entry, include a PATH record for the directory itself. A few other notable changes: - fixed audit_inode_child() hooks in fsnotify_move() - removed unused flags arg from audit_inode() - added audit log routines for logging a portion of a string Here's some sample output. before patch: type=SYSCALL msg=audit(1149821605.320:26): arch=40000003 syscall=39 success=yes exit=0 a0=bf8d3c7c a1=1ff a2=804e1b8 a3=bf8d3c7c items=1 ppid=739 pid=800 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=ttyS0 comm="mkdir" exe="/bin/mkdir" subj=root:system_r:unconfined_t:s0-s0:c0.c255 type=CWD msg=audit(1149821605.320:26): cwd="/root" type=PATH msg=audit(1149821605.320:26): item=0 name="foo" parent=164068 inode=164010 dev=03:00 mode=040755 ouid=0 ogid=0 rdev=00:00 obj=root:object_r:user_home_t:s0 after patch: type=SYSCALL msg=audit(1149822032.332:24): arch=40000003 syscall=39 success=yes exit=0 a0=bfdd9c7c a1=1ff a2=804e1b8 a3=bfdd9c7c items=2 ppid=714 pid=777 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=ttyS0 comm="mkdir" exe="/bin/mkdir" subj=root:system_r:unconfined_t:s0-s0:c0.c255 type=CWD msg=audit(1149822032.332:24): cwd="/root" type=PATH msg=audit(1149822032.332:24): item=0 name="/root" inode=164068 dev=03:00 mode=040750 ouid=0 ogid=0 rdev=00:00 obj=root:object_r:user_home_dir_t:s0 type=PATH msg=audit(1149822032.332:24): item=1 name="foo" inode=164010 dev=03:00 mode=040755 ouid=0 ogid=0 rdev=00:00 obj=root:object_r:user_home_t:s0 Signed-off-by: Amy Griffis <amy.griffis@hp.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 6a2bcee commit 9c937dc

9 files changed

Lines changed: 142 additions & 74 deletions

File tree

‎fs/namei.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ static int fastcall do_path_lookup(int dfd, const char *name,
11271127
if (likely(retval == 0)) {
11281128
if (unlikely(current->audit_context && nd && nd->dentry &&
11291129
nd->dentry->d_inode))
1130-
audit_inode(name, nd->dentry->d_inode, flags);
1130+
audit_inode(name, nd->dentry->d_inode);
11311131
}
11321132
out_fail:
11331133
return retval;

‎fs/open.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
633633
dentry = file->f_dentry;
634634
inode = dentry->d_inode;
635635

636-
audit_inode(NULL, inode, 0);
636+
audit_inode(NULL, inode);
637637

638638
err = -EROFS;
639639
if (IS_RDONLY(inode))
@@ -786,7 +786,7 @@ asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
786786
if (file) {
787787
struct dentry * dentry;
788788
dentry = file->f_dentry;
789-
audit_inode(NULL, dentry->d_inode, 0);
789+
audit_inode(NULL, dentry->d_inode);
790790
error = chown_common(dentry, user, group);
791791
fput(file);
792792
}

‎fs/xattr.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ sys_fsetxattr(int fd, char __user *name, void __user *value,
242242
if (!f)
243243
return error;
244244
dentry = f->f_dentry;
245-
audit_inode(NULL, dentry->d_inode, 0);
245+
audit_inode(NULL, dentry->d_inode);
246246
error = setxattr(dentry, name, value, size, flags);
247247
fput(f);
248248
return error;
@@ -469,7 +469,7 @@ sys_fremovexattr(int fd, char __user *name)
469469
if (!f)
470470
return error;
471471
dentry = f->f_dentry;
472-
audit_inode(NULL, dentry->d_inode, 0);
472+
audit_inode(NULL, dentry->d_inode);
473473
error = removexattr(dentry, name);
474474
fput(f);
475475
return error;

‎include/linux/audit.h‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,17 @@ extern void audit_syscall_entry(int arch,
310310
extern void audit_syscall_exit(int failed, long return_code);
311311
extern void __audit_getname(const char *name);
312312
extern void audit_putname(const char *name);
313-
extern void __audit_inode(const char *name, const struct inode *inode, unsigned flags);
313+
extern void __audit_inode(const char *name, const struct inode *inode);
314314
extern void __audit_inode_child(const char *dname, const struct inode *inode,
315315
unsigned long pino);
316316
static inline void audit_getname(const char *name)
317317
{
318318
if (unlikely(current->audit_context))
319319
__audit_getname(name);
320320
}
321-
static inline void audit_inode(const char *name, const struct inode *inode,
322-
unsigned flags) {
321+
static inline void audit_inode(const char *name, const struct inode *inode) {
323322
if (unlikely(current->audit_context))
324-
__audit_inode(name, inode, flags);
323+
__audit_inode(name, inode);
325324
}
326325
static inline void audit_inode_child(const char *dname,
327326
const struct inode *inode,
@@ -398,9 +397,9 @@ static inline int audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
398397
#define audit_syscall_exit(f,r) do { ; } while (0)
399398
#define audit_getname(n) do { ; } while (0)
400399
#define audit_putname(n) do { ; } while (0)
401-
#define __audit_inode(n,i,f) do { ; } while (0)
400+
#define __audit_inode(n,i) do { ; } while (0)
402401
#define __audit_inode_child(d,i,p) do { ; } while (0)
403-
#define audit_inode(n,i,f) do { ; } while (0)
402+
#define audit_inode(n,i) do { ; } while (0)
404403
#define audit_inode_child(d,i,p) do { ; } while (0)
405404
#define auditsc_get_stamp(c,t,s) do { BUG(); } while (0)
406405
#define audit_get_loginuid(c) ({ -1; })
@@ -435,6 +434,9 @@ extern void audit_log_hex(struct audit_buffer *ab,
435434
size_t len);
436435
extern const char * audit_log_untrustedstring(struct audit_buffer *ab,
437436
const char *string);
437+
extern const char * audit_log_n_untrustedstring(struct audit_buffer *ab,
438+
size_t n,
439+
const char *string);
438440
extern void audit_log_d_path(struct audit_buffer *ab,
439441
const char *prefix,
440442
struct dentry *dentry,
@@ -452,6 +454,7 @@ extern int audit_receive_filter(int type, int pid, int uid, int seq,
452454
#define audit_log_end(b) do { ; } while (0)
453455
#define audit_log_hex(a,b,l) do { ; } while (0)
454456
#define audit_log_untrustedstring(a,s) do { ; } while (0)
457+
#define audit_log_n_untrustedstring(a,n,s) do { ; } while (0)
455458
#define audit_log_d_path(b,p,d,v) do { ; } while (0)
456459
#endif
457460
#endif

‎include/linux/fsnotify.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
6767
if (source) {
6868
inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
6969
}
70-
audit_inode_child(old_name, source, old_dir->i_ino);
71-
audit_inode_child(new_name, target, new_dir->i_ino);
70+
audit_inode_child(new_name, source, new_dir->i_ino);
7271
}
7372

7473
/*

‎kernel/audit.c‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,20 +1051,53 @@ void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
10511051
skb_put(skb, len << 1); /* new string is twice the old string */
10521052
}
10531053

1054+
/*
1055+
* Format a string of no more than slen characters into the audit buffer,
1056+
* enclosed in quote marks.
1057+
*/
1058+
static void audit_log_n_string(struct audit_buffer *ab, size_t slen,
1059+
const char *string)
1060+
{
1061+
int avail, new_len;
1062+
unsigned char *ptr;
1063+
struct sk_buff *skb;
1064+
1065+
BUG_ON(!ab->skb);
1066+
skb = ab->skb;
1067+
avail = skb_tailroom(skb);
1068+
new_len = slen + 3; /* enclosing quotes + null terminator */
1069+
if (new_len > avail) {
1070+
avail = audit_expand(ab, new_len);
1071+
if (!avail)
1072+
return;
1073+
}
1074+
ptr = skb->tail;
1075+
*ptr++ = '"';
1076+
memcpy(ptr, string, slen);
1077+
ptr += slen;
1078+
*ptr++ = '"';
1079+
*ptr = 0;
1080+
skb_put(skb, slen + 2); /* don't include null terminator */
1081+
}
1082+
10541083
/**
1055-
* audit_log_unstrustedstring - log a string that may contain random characters
1084+
* audit_log_n_unstrustedstring - log a string that may contain random characters
10561085
* @ab: audit_buffer
1086+
* @len: lenth of string (not including trailing null)
10571087
* @string: string to be logged
10581088
*
10591089
* This code will escape a string that is passed to it if the string
10601090
* contains a control character, unprintable character, double quote mark,
10611091
* or a space. Unescaped strings will start and end with a double quote mark.
10621092
* Strings that are escaped are printed in hex (2 digits per char).
1093+
*
1094+
* The caller specifies the number of characters in the string to log, which may
1095+
* or may not be the entire string.
10631096
*/
1064-
const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
1097+
const char *audit_log_n_untrustedstring(struct audit_buffer *ab, size_t len,
1098+
const char *string)
10651099
{
10661100
const unsigned char *p = string;
1067-
size_t len = strlen(string);
10681101

10691102
while (*p) {
10701103
if (*p == '"' || *p < 0x21 || *p > 0x7f) {
@@ -1073,10 +1106,23 @@ const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *strin
10731106
}
10741107
p++;
10751108
}
1076-
audit_log_format(ab, "\"%s\"", string);
1109+
audit_log_n_string(ab, len, string);
10771110
return p + 1;
10781111
}
10791112

1113+
/**
1114+
* audit_log_unstrustedstring - log a string that may contain random characters
1115+
* @ab: audit_buffer
1116+
* @string: string to be logged
1117+
*
1118+
* Same as audit_log_n_unstrustedstring(), except that strlen is used to
1119+
* determine string length.
1120+
*/
1121+
const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
1122+
{
1123+
return audit_log_n_untrustedstring(ab, strlen(string), string);
1124+
}
1125+
10801126
/* This is a helper-function to print the escaped d_path */
10811127
void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
10821128
struct dentry *dentry, struct vfsmount *vfsmnt)

‎kernel/audit.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ static inline int audit_hash_ino(u32 ino)
104104
}
105105

106106
extern int audit_comparator(const u32 left, const u32 op, const u32 right);
107-
extern int audit_compare_dname_path(const char *dname, const char *path);
107+
extern int audit_compare_dname_path(const char *dname, const char *path,
108+
int *dirlen);
108109
extern struct sk_buff * audit_make_reply(int pid, int seq, int type,
109110
int done, int multi,
110111
void *payload, int size);

‎kernel/auditfilter.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ static void audit_update_watch(struct audit_parent *parent,
787787

788788
mutex_lock(&audit_filter_mutex);
789789
list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
790-
if (audit_compare_dname_path(dname, owatch->path))
790+
if (audit_compare_dname_path(dname, owatch->path, NULL))
791791
continue;
792792

793793
/* If the update involves invalidating rules, do the inode-based
@@ -1387,7 +1387,8 @@ int audit_comparator(const u32 left, const u32 op, const u32 right)
13871387

13881388
/* Compare given dentry name with last component in given path,
13891389
* return of 0 indicates a match. */
1390-
int audit_compare_dname_path(const char *dname, const char *path)
1390+
int audit_compare_dname_path(const char *dname, const char *path,
1391+
int *dirlen)
13911392
{
13921393
int dlen, plen;
13931394
const char *p;
@@ -1416,6 +1417,9 @@ int audit_compare_dname_path(const char *dname, const char *path)
14161417
p++;
14171418
}
14181419

1420+
/* return length of path's directory component */
1421+
if (dirlen)
1422+
*dirlen = p - path;
14191423
return strncmp(p, dname, dlen);
14201424
}
14211425

0 commit comments

Comments
 (0)