···1083108310841084source kernel/power/Kconfig1085108510861086+config SECCOMP10871087+ bool "Enable seccomp to safely compute untrusted bytecode"10881088+ depends on PROC_FS10891089+ default y10901090+ help10911091+ This kernel feature is useful for number crunching applications10921092+ that may need to compute untrusted bytecode during their10931093+ execution. By using pipes or other transports made available to10941094+ the process as file descriptors supporting the read/write10951095+ syscalls, it's possible to isolate those applications in10961096+ their own address space using seccomp. Once seccomp is10971097+ enabled via /proc/<pid>/seccomp, it cannot be disabled10981098+ and the task is only allowed to execute a few safe syscalls10991099+ defined by each seccomp mode.11001100+11011101+ If unsure, say Y. Only embedded should say N here.11021102+10861103endmenu1087110410881105config ISA_DMA_API
···7777#define TIF_POLLING_NRFLAG 4 /* true if poll_idle() is polling7878 TIF_NEED_RESCHED */7979#define TIF_MEMDIE 58080+#define TIF_SYSCALL_AUDIT 6 /* syscall auditing active */8181+#define TIF_SECCOMP 7 /* secure computing */8282+8083/* as above, but as bit values */8184#define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)8285#define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)8386#define _TIF_SIGPENDING (1<<TIF_SIGPENDING)8487#define _TIF_NEED_RESCHED (1<<TIF_NEED_RESCHED)8588#define _TIF_POLLING_NRFLAG (1<<TIF_POLLING_NRFLAG)8989+#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)9090+#define _TIF_SECCOMP (1<<TIF_SECCOMP)9191+9292+#define _TIF_SYSCALL_T_OR_A (_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SECCOMP)86938794/*8895 * Non racy (local) flags bit numbers
+70-24
include/linux/audit.h
···2727#include <linux/sched.h>2828#include <linux/elf.h>29293030-/* Request and reply types */3131-#define AUDIT_GET 1000 /* Get status */3232-#define AUDIT_SET 1001 /* Set status (enable/disable/auditd) */3333-#define AUDIT_LIST 1002 /* List filtering rules */3434-#define AUDIT_ADD 1003 /* Add filtering rule */3535-#define AUDIT_DEL 1004 /* Delete filtering rule */3636-#define AUDIT_USER 1005 /* Send a message from user-space */3737-#define AUDIT_LOGIN 1006 /* Define the login id and informaiton */3838-#define AUDIT_KERNEL 2000 /* Asynchronous audit record. NOT A REQUEST. */3030+/* The netlink messages for the audit system is divided into blocks:3131+ * 1000 - 1099 are for commanding the audit system3232+ * 1100 - 1199 user space trusted application messages3333+ * 1200 - 1299 messages internal to the audit daemon3434+ * 1300 - 1399 audit event messages3535+ * 1400 - 1499 SE Linux use3636+ * 1500 - 1999 future use3737+ * 2000 is for otherwise unclassified kernel audit messages3838+ *3939+ * Messages from 1000-1199 are bi-directional. 1200-1299 are exclusively user4040+ * space. Anything over that is kernel --> user space communication.4141+ */4242+#define AUDIT_GET 1000 /* Get status */4343+#define AUDIT_SET 1001 /* Set status (enable/disable/auditd) */4444+#define AUDIT_LIST 1002 /* List syscall filtering rules */4545+#define AUDIT_ADD 1003 /* Add syscall filtering rule */4646+#define AUDIT_DEL 1004 /* Delete syscall filtering rule */4747+#define AUDIT_USER 1005 /* Message from userspace -- deprecated */4848+#define AUDIT_LOGIN 1006 /* Define the login id and information */4949+#define AUDIT_WATCH_INS 1007 /* Insert file/dir watch entry */5050+#define AUDIT_WATCH_REM 1008 /* Remove file/dir watch entry */5151+#define AUDIT_WATCH_LIST 1009 /* List all file/dir watches */5252+#define AUDIT_SIGNAL_INFO 1010 /* Get info about sender of signal to auditd */5353+5454+#define AUDIT_FIRST_USER_MSG 1100 /* Userspace messages uninteresting to kernel */5555+#define AUDIT_LAST_USER_MSG 11995656+5757+#define AUDIT_DAEMON_START 1200 /* Daemon startup record */5858+#define AUDIT_DAEMON_END 1201 /* Daemon normal stop record */5959+#define AUDIT_DAEMON_ABORT 1202 /* Daemon error stop record */6060+#define AUDIT_DAEMON_CONFIG 1203 /* Daemon config change */6161+6262+#define AUDIT_SYSCALL 1300 /* Syscall event */6363+#define AUDIT_FS_WATCH 1301 /* Filesystem watch event */6464+#define AUDIT_PATH 1302 /* Filename path information */6565+#define AUDIT_IPC 1303 /* IPC record */6666+#define AUDIT_SOCKETCALL 1304 /* sys_socketcall arguments */6767+#define AUDIT_CONFIG_CHANGE 1305 /* Audit system configuration change */6868+#define AUDIT_SOCKADDR 1306 /* sockaddr copied as syscall arg */6969+#define AUDIT_CWD 1307 /* Current working directory */7070+7171+#define AUDIT_AVC 1400 /* SE Linux avc denial or grant */7272+#define AUDIT_SELINUX_ERR 1401 /* Internal SE Linux Errors */7373+#define AUDIT_AVC_PATH 1402 /* dentry, vfsmount pair from avc */7474+7575+#define AUDIT_KERNEL 2000 /* Asynchronous audit record. NOT A REQUEST. */39764077/* Rule flags */4178#define AUDIT_PER_TASK 0x01 /* Apply rule at task creation (not syscall) */···169132#define AUDIT_ARCH_V850 (EM_V850|__AUDIT_ARCH_LE)170133#define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)171134172172-#ifndef __KERNEL__173173-struct audit_message {174174- struct nlmsghdr nlh;175175- char data[1200];176176-};177177-#endif178178-179135struct audit_status {180136 __u32 mask; /* Bit mask for valid entries */181181- __u32 enabled; /* 1 = enabled, 0 = disbaled */137137+ __u32 enabled; /* 1 = enabled, 0 = disabled */182138 __u32 failure; /* Failure-to-log action */183139 __u32 pid; /* pid of auditd process */184140 __u32 rate_limit; /* messages rate limit (per second) */···190160};191161192162#ifdef __KERNEL__163163+164164+struct audit_sig_info {165165+ uid_t uid;166166+ pid_t pid;167167+};193168194169struct audit_buffer;195170struct audit_context;···220185 /* Private API (for audit.c only) */221186extern int audit_receive_filter(int type, int pid, int uid, int seq,222187 void *data, uid_t loginuid);223223-extern void audit_get_stamp(struct audit_context *ctx,224224- struct timespec *t, unsigned int *serial);188188+extern unsigned int audit_serial(void);189189+extern void auditsc_get_stamp(struct audit_context *ctx,190190+ struct timespec *t, unsigned int *serial);225191extern int audit_set_loginuid(struct task_struct *task, uid_t loginuid);226192extern uid_t audit_get_loginuid(struct audit_context *ctx);227193extern int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode);194194+extern int audit_socketcall(int nargs, unsigned long *args);195195+extern int audit_sockaddr(int len, void *addr);196196+extern int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt);197197+extern void audit_signal_info(int sig, struct task_struct *t);228198#else229199#define audit_alloc(t) ({ 0; })230200#define audit_free(t) do { ; } while (0)···238198#define audit_getname(n) do { ; } while (0)239199#define audit_putname(n) do { ; } while (0)240200#define audit_inode(n,i) do { ; } while (0)201201+#define audit_receive_filter(t,p,u,s,d,l) ({ -EOPNOTSUPP; })202202+#define auditsc_get_stamp(c,t,s) do { BUG(); } while (0)241203#define audit_get_loginuid(c) ({ -1; })242204#define audit_ipc_perms(q,u,g,m) ({ 0; })205205+#define audit_socketcall(n,a) ({ 0; })206206+#define audit_sockaddr(len, addr) ({ 0; })207207+#define audit_avc_path(dentry, mnt) ({ 0; })208208+#define audit_signal_info(s,t) do { ; } while (0)243209#endif244210245211#ifdef CONFIG_AUDIT246212/* These are defined in audit.c */247213 /* Public API */248248-extern void audit_log(struct audit_context *ctx,214214+extern void audit_log(struct audit_context *ctx, int type,249215 const char *fmt, ...)250250- __attribute__((format(printf,2,3)));216216+ __attribute__((format(printf,3,4)));251217252252-extern struct audit_buffer *audit_log_start(struct audit_context *ctx);218218+extern struct audit_buffer *audit_log_start(struct audit_context *ctx,int type);253219extern void audit_log_format(struct audit_buffer *ab,254220 const char *fmt, ...)255221 __attribute__((format(printf,2,3)));···275229 void *payload, int size);276230extern void audit_log_lost(const char *message);277231#else278278-#define audit_log(t,f,...) do { ; } while (0)279279-#define audit_log_start(t) ({ NULL; })232232+#define audit_log(c,t,f,...) do { ; } while (0)233233+#define audit_log_start(c,t) ({ NULL; })280234#define audit_log_vformat(b,f,a) do { ; } while (0)281235#define audit_log_format(b,f,...) do { ; } while (0)282236#define audit_log_end(b) do { ; } while (0)
+2-1
init/Kconfig
···164164165165config AUDIT166166 bool "Auditing support"167167+ depends on NET167168 default y if SECURITY_SELINUX168169 help169170 Enable auditing infrastructure that can be used with another···174173175174config AUDITSYSCALL176175 bool "Enable system-call auditing support"177177- depends on AUDIT && (X86 || PPC64 || ARCH_S390 || IA64 || UML)176176+ depends on AUDIT && (X86 || PPC || PPC64 || ARCH_S390 || IA64 || UML)178177 default y if SECURITY_SELINUX179178 help180179 Enable low-overhead system-call auditing infrastructure that
+306-305
kernel/audit.c
···4646#include <asm/types.h>4747#include <linux/mm.h>4848#include <linux/module.h>4949+#include <linux/err.h>5050+#include <linux/kthread.h>49515052#include <linux/audit.h>5153···70687169/* If audit records are to be written to the netlink socket, audit_pid7270 * contains the (non-zero) pid. */7373-static int audit_pid;7171+int audit_pid;74727573/* If audit_limit is non-zero, limit the rate of sending audit records7674 * to that number per second. This prevents DoS attacks, but results in···79778078/* Number of outstanding audit_buffers allowed. */8179static int audit_backlog_limit = 64;8282-static atomic_t audit_backlog = ATOMIC_INIT(0);8080+8181+/* The identity of the user shutting down the audit system. */8282+uid_t audit_sig_uid = -1;8383+pid_t audit_sig_pid = -1;83848485/* Records can be lost in several ways:8586 0) [suppressed in audit_alloc]···9691/* The netlink socket. */9792static struct sock *audit_sock;98939999-/* There are two lists of audit buffers. The txlist contains audit100100- * buffers that cannot be sent immediately to the netlink device because101101- * we are in an irq context (these are sent later in a tasklet).102102- *103103- * The second list is a list of pre-allocated audit buffers (if more9494+/* The audit_freelist is a list of pre-allocated audit buffers (if more10495 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of10596 * being placed on the freelist). */106106-static DEFINE_SPINLOCK(audit_txlist_lock);10797static DEFINE_SPINLOCK(audit_freelist_lock);10898static int audit_freelist_count = 0;109109-static LIST_HEAD(audit_txlist);11099static LIST_HEAD(audit_freelist);100100+101101+static struct sk_buff_head audit_skb_queue;102102+static struct task_struct *kauditd_task;103103+static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);111104112105/* There are three lists of rules -- one to search at task creation113106 * time, one to search at syscall entry time, and another to search at···115112static LIST_HEAD(audit_extlist);116113117114/* The netlink socket is only to be read by 1 CPU, which lets us assume118118- * that list additions and deletions never happen simultaneiously in115115+ * that list additions and deletions never happen simultaneously in119116 * auditsc.c */120117static DECLARE_MUTEX(audit_netlink_sem);121118···135132 * use simultaneously. */136133struct audit_buffer {137134 struct list_head list;138138- struct sk_buff_head sklist; /* formatted skbs ready to send */135135+ struct sk_buff *skb; /* formatted skb ready to send */139136 struct audit_context *ctx; /* NULL or associated context */140140- int len; /* used area of tmp */141141- char tmp[AUDIT_BUFSIZ];142142-143143- /* Pointer to header and contents */144144- struct nlmsghdr *nlh;145145- int total;146146- int type;147147- int pid;148137};149138150150-void audit_set_type(struct audit_buffer *ab, int type)139139+static void audit_set_pid(struct audit_buffer *ab, pid_t pid)151140{152152- ab->type = type;141141+ struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;142142+ nlh->nlmsg_pid = pid;153143}154144155145struct audit_entry {156146 struct list_head list;157147 struct audit_rule rule;158148};159159-160160-static void audit_log_end_irq(struct audit_buffer *ab);161161-static void audit_log_end_fast(struct audit_buffer *ab);162149163150static void audit_panic(const char *message)164151{···220227221228 if (print) {222229 printk(KERN_WARNING223223- "audit: audit_lost=%d audit_backlog=%d"224224- " audit_rate_limit=%d audit_backlog_limit=%d\n",230230+ "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",225231 atomic_read(&audit_lost),226226- atomic_read(&audit_backlog),227232 audit_rate_limit,228233 audit_backlog_limit);229234 audit_panic(message);···233242{234243 int old = audit_rate_limit;235244 audit_rate_limit = limit;236236- audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u",245245+ audit_log(NULL, AUDIT_CONFIG_CHANGE, 246246+ "audit_rate_limit=%d old=%d by auid=%u",237247 audit_rate_limit, old, loginuid);238248 return old;239249}···243251{244252 int old = audit_backlog_limit;245253 audit_backlog_limit = limit;246246- audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u",254254+ audit_log(NULL, AUDIT_CONFIG_CHANGE,255255+ "audit_backlog_limit=%d old=%d by auid=%u",247256 audit_backlog_limit, old, loginuid);248257 return old;249258}···255262 if (state != 0 && state != 1)256263 return -EINVAL;257264 audit_enabled = state;258258- audit_log(NULL, "audit_enabled=%d old=%d by auid %u",259259- audit_enabled, old, loginuid);265265+ audit_log(NULL, AUDIT_CONFIG_CHANGE,266266+ "audit_enabled=%d old=%d by auid=%u",267267+ audit_enabled, old, loginuid);260268 return old;261269}262270···269275 && state != AUDIT_FAIL_PANIC)270276 return -EINVAL;271277 audit_failure = state;272272- audit_log(NULL, "audit_failure=%d old=%d by auid %u",273273- audit_failure, old, loginuid);278278+ audit_log(NULL, AUDIT_CONFIG_CHANGE,279279+ "audit_failure=%d old=%d by auid=%u",280280+ audit_failure, old, loginuid);274281 return old;275282}276283277277-#ifdef CONFIG_NET284284+int kauditd_thread(void *dummy)285285+{286286+ struct sk_buff *skb;287287+288288+ while (1) {289289+ skb = skb_dequeue(&audit_skb_queue);290290+ if (skb) {291291+ if (audit_pid) {292292+ int err = netlink_unicast(audit_sock, skb, audit_pid, 0);293293+ if (err < 0) {294294+ BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */295295+ printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);296296+ audit_pid = 0;297297+ }298298+ } else {299299+ printk(KERN_ERR "%s\n", skb->data + NLMSG_SPACE(0));300300+ kfree_skb(skb);301301+ }302302+ } else {303303+ DECLARE_WAITQUEUE(wait, current);304304+ set_current_state(TASK_INTERRUPTIBLE);305305+ add_wait_queue(&kauditd_wait, &wait);306306+307307+ if (!skb_queue_len(&audit_skb_queue))308308+ schedule();309309+310310+ __set_current_state(TASK_RUNNING);311311+ remove_wait_queue(&kauditd_wait, &wait);312312+ }313313+ }314314+}315315+278316void audit_send_reply(int pid, int seq, int type, int done, int multi,279317 void *payload, int size)280318{···319293320294 skb = alloc_skb(len, GFP_KERNEL);321295 if (!skb)322322- goto nlmsg_failure;296296+ return;323297324324- nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));298298+ nlh = NLMSG_PUT(skb, pid, seq, t, size);325299 nlh->nlmsg_flags = flags;326300 data = NLMSG_DATA(nlh);327301 memcpy(data, payload, size);328328- netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);302302+303303+ /* Ignore failure. It'll only happen if the sender goes away,304304+ because our timeout is set to infinite. */305305+ netlink_unicast(audit_sock, skb, pid, 0);329306 return;330307331308nlmsg_failure: /* Used by NLMSG_PUT */···350321 case AUDIT_SET:351322 case AUDIT_ADD:352323 case AUDIT_DEL:324324+ case AUDIT_SIGNAL_INFO:353325 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))354326 err = -EPERM;355327 break;356328 case AUDIT_USER:329329+ case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:357330 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))358331 err = -EPERM;359332 break;···375344 struct audit_buffer *ab;376345 u16 msg_type = nlh->nlmsg_type;377346 uid_t loginuid; /* loginuid of sender */347347+ struct audit_sig_info sig_data;378348379349 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);380350 if (err)381351 return err;352352+353353+ /* As soon as there's any sign of userspace auditd, start kauditd to talk to it */354354+ if (!kauditd_task)355355+ kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");356356+ if (IS_ERR(kauditd_task)) {357357+ err = PTR_ERR(kauditd_task);358358+ kauditd_task = NULL;359359+ return err;360360+ }382361383362 pid = NETLINK_CREDS(skb)->pid;384363 uid = NETLINK_CREDS(skb)->uid;···404363 status_set.rate_limit = audit_rate_limit;405364 status_set.backlog_limit = audit_backlog_limit;406365 status_set.lost = atomic_read(&audit_lost);407407- status_set.backlog = atomic_read(&audit_backlog);366366+ status_set.backlog = skb_queue_len(&audit_skb_queue);408367 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,409368 &status_set, sizeof(status_set));410369 break;···423382 if (status_get->mask & AUDIT_STATUS_PID) {424383 int old = audit_pid;425384 audit_pid = status_get->pid;426426- audit_log(NULL, "audit_pid=%d old=%d by auid %u",385385+ audit_log(NULL, AUDIT_CONFIG_CHANGE,386386+ "audit_pid=%d old=%d by auid=%u",427387 audit_pid, old, loginuid);428388 }429389 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)···434392 loginuid);435393 break;436394 case AUDIT_USER:437437- ab = audit_log_start(NULL);395395+ case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:396396+ ab = audit_log_start(NULL, msg_type);438397 if (!ab)439398 break; /* audit_panic has been called */440399 audit_log_format(ab,441441- "user pid=%d uid=%d length=%d loginuid=%u"400400+ "user pid=%d uid=%u auid=%u"442401 " msg='%.1024s'",443443- pid, uid,444444- (int)(nlh->nlmsg_len445445- - ((char *)data - (char *)nlh)),446446- loginuid, (char *)data);447447- ab->type = AUDIT_USER;448448- ab->pid = pid;402402+ pid, uid, loginuid, (char *)data);403403+ audit_set_pid(ab, pid);449404 audit_log_end(ab);450405 break;451406 case AUDIT_ADD:···451412 return -EINVAL;452413 /* fallthrough */453414 case AUDIT_LIST:454454-#ifdef CONFIG_AUDITSYSCALL455415 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,456416 uid, seq, data, loginuid);457457-#else458458- err = -EOPNOTSUPP;459459-#endif417417+ break;418418+ case AUDIT_SIGNAL_INFO:419419+ sig_data.uid = audit_sig_uid;420420+ sig_data.pid = audit_sig_pid;421421+ audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO, 422422+ 0, 0, &sig_data, sizeof(sig_data));460423 break;461424 default:462425 err = -EINVAL;···508467 up(&audit_netlink_sem);509468}510469511511-/* Move data from tmp buffer into an skb. This is an extra copy, and512512- * that is unfortunate. However, the copy will only occur when a record513513- * is being written to user space, which is already a high-overhead514514- * operation. (Elimination of the copy is possible, for example, by515515- * writing directly into a pre-allocated skb, at the cost of wasting516516- * memory. */517517-static void audit_log_move(struct audit_buffer *ab)518518-{519519- struct sk_buff *skb;520520- char *start;521521- int extra = ab->nlh ? 0 : NLMSG_SPACE(0);522522-523523- /* possible resubmission */524524- if (ab->len == 0)525525- return;526526-527527- skb = skb_peek_tail(&ab->sklist);528528- if (!skb || skb_tailroom(skb) <= ab->len + extra) {529529- skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC);530530- if (!skb) {531531- ab->len = 0; /* Lose information in ab->tmp */532532- audit_log_lost("out of memory in audit_log_move");533533- return;534534- }535535- __skb_queue_tail(&ab->sklist, skb);536536- if (!ab->nlh)537537- ab->nlh = (struct nlmsghdr *)skb_put(skb,538538- NLMSG_SPACE(0));539539- }540540- start = skb_put(skb, ab->len);541541- memcpy(start, ab->tmp, ab->len);542542- ab->len = 0;543543-}544544-545545-/* Iterate over the skbuff in the audit_buffer, sending their contents546546- * to user space. */547547-static inline int audit_log_drain(struct audit_buffer *ab)548548-{549549- struct sk_buff *skb;550550-551551- while ((skb = skb_dequeue(&ab->sklist))) {552552- int retval = 0;553553-554554- if (audit_pid) {555555- if (ab->nlh) {556556- ab->nlh->nlmsg_len = ab->total;557557- ab->nlh->nlmsg_type = ab->type;558558- ab->nlh->nlmsg_flags = 0;559559- ab->nlh->nlmsg_seq = 0;560560- ab->nlh->nlmsg_pid = ab->pid;561561- }562562- skb_get(skb); /* because netlink_* frees */563563- retval = netlink_unicast(audit_sock, skb, audit_pid,564564- MSG_DONTWAIT);565565- }566566- if (retval == -EAGAIN &&567567- (atomic_read(&audit_backlog)) < audit_backlog_limit) {568568- skb_queue_head(&ab->sklist, skb);569569- audit_log_end_irq(ab);570570- return 1;571571- }572572- if (retval < 0) {573573- if (retval == -ECONNREFUSED) {574574- printk(KERN_ERR575575- "audit: *NO* daemon at audit_pid=%d\n",576576- audit_pid);577577- audit_pid = 0;578578- } else579579- audit_log_lost("netlink socket too busy");580580- }581581- if (!audit_pid) { /* No daemon */582582- int offset = ab->nlh ? NLMSG_SPACE(0) : 0;583583- int len = skb->len - offset;584584- skb->data[offset + len] = '\0';585585- printk(KERN_ERR "%s\n", skb->data + offset);586586- }587587- kfree_skb(skb);588588- ab->nlh = NULL;589589- }590590- return 0;591591-}592470593471/* Initialize audit support at boot time. */594472static int __init audit_init(void)···518558 if (!audit_sock)519559 audit_panic("cannot initialize netlink socket");520560561561+ audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;562562+ skb_queue_head_init(&audit_skb_queue);521563 audit_initialized = 1;522564 audit_enabled = audit_default;523523- audit_log(NULL, "initialized");565565+ audit_log(NULL, AUDIT_KERNEL, "initialized");524566 return 0;525567}526526-527527-#else528528-/* Without CONFIG_NET, we have no skbuffs. For now, print what we have529529- * in the buffer. */530530-static void audit_log_move(struct audit_buffer *ab)531531-{532532- printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp);533533- ab->len = 0;534534-}535535-536536-static inline int audit_log_drain(struct audit_buffer *ab)537537-{538538- return 0;539539-}540540-541541-/* Initialize audit support at boot time. */542542-int __init audit_init(void)543543-{544544- printk(KERN_INFO "audit: initializing WITHOUT netlink support\n");545545- audit_sock = NULL;546546- audit_pid = 0;547547-548548- audit_initialized = 1;549549- audit_enabled = audit_default;550550- audit_log(NULL, "initialized");551551- return 0;552552-}553553-#endif554554-555568__initcall(audit_init);556569557570/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */···541608542609__setup("audit=", audit_enable);543610544544-545545-/* Obtain an audit buffer. This routine does locking to obtain the546546- * audit buffer, but then no locking is required for calls to547547- * audit_log_*format. If the tsk is a task that is currently in a548548- * syscall, then the syscall is marked as auditable and an audit record549549- * will be written at syscall exit. If there is no associated task, tsk550550- * should be NULL. */551551-struct audit_buffer *audit_log_start(struct audit_context *ctx)611611+static void audit_buffer_free(struct audit_buffer *ab)552612{553553- struct audit_buffer *ab = NULL;554554- unsigned long flags;555555- struct timespec t;556556- unsigned int serial;613613+ unsigned long flags;557614558558- if (!audit_initialized)559559- return NULL;615615+ if (!ab)616616+ return;560617561561- if (audit_backlog_limit562562- && atomic_read(&audit_backlog) > audit_backlog_limit) {563563- if (audit_rate_check())564564- printk(KERN_WARNING565565- "audit: audit_backlog=%d > "566566- "audit_backlog_limit=%d\n",567567- atomic_read(&audit_backlog),568568- audit_backlog_limit);569569- audit_log_lost("backlog limit exceeded");570570- return NULL;571571- }618618+ if (ab->skb)619619+ kfree_skb(ab->skb);620620+621621+ spin_lock_irqsave(&audit_freelist_lock, flags);622622+ if (++audit_freelist_count > AUDIT_MAXFREE)623623+ kfree(ab);624624+ else625625+ list_add(&ab->list, &audit_freelist);626626+ spin_unlock_irqrestore(&audit_freelist_lock, flags);627627+}628628+629629+static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,630630+ int gfp_mask, int type)631631+{632632+ unsigned long flags;633633+ struct audit_buffer *ab = NULL;634634+ struct nlmsghdr *nlh;572635573636 spin_lock_irqsave(&audit_freelist_lock, flags);574637 if (!list_empty(&audit_freelist)) {···575646 }576647 spin_unlock_irqrestore(&audit_freelist_lock, flags);577648578578- if (!ab)579579- ab = kmalloc(sizeof(*ab), GFP_ATOMIC);649649+ if (!ab) {650650+ ab = kmalloc(sizeof(*ab), gfp_mask);651651+ if (!ab)652652+ goto err;653653+ }654654+655655+ ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);656656+ if (!ab->skb)657657+ goto err;658658+659659+ ab->ctx = ctx;660660+ nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));661661+ nlh->nlmsg_type = type;662662+ nlh->nlmsg_flags = 0;663663+ nlh->nlmsg_pid = 0;664664+ nlh->nlmsg_seq = 0;665665+ return ab;666666+err:667667+ audit_buffer_free(ab);668668+ return NULL;669669+}670670+671671+/* Compute a serial number for the audit record. Audit records are672672+ * written to user-space as soon as they are generated, so a complete673673+ * audit record may be written in several pieces. The timestamp of the674674+ * record and this serial number are used by the user-space tools to675675+ * determine which pieces belong to the same audit record. The676676+ * (timestamp,serial) tuple is unique for each syscall and is live from677677+ * syscall entry to syscall exit.678678+ *679679+ * Atomic values are only guaranteed to be 24-bit, so we count down.680680+ *681681+ * NOTE: Another possibility is to store the formatted records off the682682+ * audit context (for those records that have a context), and emit them683683+ * all at syscall exit. However, this could delay the reporting of684684+ * significant errors until syscall exit (or never, if the system685685+ * halts). */686686+unsigned int audit_serial(void)687687+{688688+ static atomic_t serial = ATOMIC_INIT(0xffffff);689689+ unsigned int a, b;690690+691691+ do {692692+ a = atomic_read(&serial);693693+ if (atomic_dec_and_test(&serial))694694+ atomic_set(&serial, 0xffffff);695695+ b = atomic_read(&serial);696696+ } while (b != a - 1);697697+698698+ return 0xffffff - b;699699+}700700+701701+static inline void audit_get_stamp(struct audit_context *ctx, 702702+ struct timespec *t, unsigned int *serial)703703+{704704+ if (ctx)705705+ auditsc_get_stamp(ctx, t, serial);706706+ else {707707+ *t = CURRENT_TIME;708708+ *serial = audit_serial();709709+ }710710+}711711+712712+/* Obtain an audit buffer. This routine does locking to obtain the713713+ * audit buffer, but then no locking is required for calls to714714+ * audit_log_*format. If the tsk is a task that is currently in a715715+ * syscall, then the syscall is marked as auditable and an audit record716716+ * will be written at syscall exit. If there is no associated task, tsk717717+ * should be NULL. */718718+struct audit_buffer *audit_log_start(struct audit_context *ctx, int type)719719+{720720+ struct audit_buffer *ab = NULL;721721+ struct timespec t;722722+ unsigned int serial;723723+724724+ if (!audit_initialized)725725+ return NULL;726726+727727+ if (audit_backlog_limit728728+ && skb_queue_len(&audit_skb_queue) > audit_backlog_limit) {729729+ if (audit_rate_check())730730+ printk(KERN_WARNING731731+ "audit: audit_backlog=%d > "732732+ "audit_backlog_limit=%d\n",733733+ skb_queue_len(&audit_skb_queue),734734+ audit_backlog_limit);735735+ audit_log_lost("backlog limit exceeded");736736+ return NULL;737737+ }738738+739739+ ab = audit_buffer_alloc(ctx, GFP_ATOMIC, type);580740 if (!ab) {581741 audit_log_lost("out of memory in audit_log_start");582742 return NULL;583743 }584744585585- atomic_inc(&audit_backlog);586586- skb_queue_head_init(&ab->sklist);745745+ audit_get_stamp(ab->ctx, &t, &serial);587746588588- ab->ctx = ctx;589589- ab->len = 0;590590- ab->nlh = NULL;591591- ab->total = 0;592592- ab->type = AUDIT_KERNEL;593593- ab->pid = 0;594594-595595-#ifdef CONFIG_AUDITSYSCALL596596- if (ab->ctx)597597- audit_get_stamp(ab->ctx, &t, &serial);598598- else599599-#endif600600- {601601- t = CURRENT_TIME;602602- serial = 0;603603- }604747 audit_log_format(ab, "audit(%lu.%03lu:%u): ",605748 t.tv_sec, t.tv_nsec/1000000, serial);606749 return ab;607750}608751752752+/**753753+ * audit_expand - expand skb in the audit buffer754754+ * @ab: audit_buffer755755+ *756756+ * Returns 0 (no space) on failed expansion, or available space if757757+ * successful.758758+ */759759+static inline int audit_expand(struct audit_buffer *ab, int extra)760760+{761761+ struct sk_buff *skb = ab->skb;762762+ int ret = pskb_expand_head(skb, skb_headroom(skb), extra,763763+ GFP_ATOMIC);764764+ if (ret < 0) {765765+ audit_log_lost("out of memory in audit_expand");766766+ return 0;767767+ }768768+ return skb_tailroom(skb);769769+}609770610771/* Format an audit message into the audit buffer. If there isn't enough611772 * room in the audit buffer, more room will be allocated and vsnprint···705686 va_list args)706687{707688 int len, avail;689689+ struct sk_buff *skb;690690+ va_list args2;708691709692 if (!ab)710693 return;711694712712- avail = sizeof(ab->tmp) - ab->len;713713- if (avail <= 0) {714714- audit_log_move(ab);715715- avail = sizeof(ab->tmp) - ab->len;695695+ BUG_ON(!ab->skb);696696+ skb = ab->skb;697697+ avail = skb_tailroom(skb);698698+ if (avail == 0) {699699+ avail = audit_expand(ab, AUDIT_BUFSIZ);700700+ if (!avail)701701+ goto out;716702 }717717- len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);703703+ va_copy(args2, args);704704+ len = vsnprintf(skb->tail, avail, fmt, args);718705 if (len >= avail) {719706 /* The printk buffer is 1024 bytes long, so if we get720707 * here and AUDIT_BUFSIZ is at least 1024, then we can721708 * log everything that printk could have logged. */722722- audit_log_move(ab);723723- avail = sizeof(ab->tmp) - ab->len;724724- len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);709709+ avail = audit_expand(ab, max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));710710+ if (!avail)711711+ goto out;712712+ len = vsnprintf(skb->tail, avail, fmt, args2);725713 }726726- ab->len += (len < avail) ? len : avail;727727- ab->total += (len < avail) ? len : avail;714714+ if (len > 0)715715+ skb_put(skb, len);716716+out:717717+ return;728718}729719730720/* Format a message into the audit buffer. All the work is done in···749721 va_end(args);750722}751723752752-void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)724724+/* This function will take the passed buf and convert it into a string of725725+ * ascii hex digits. The new string is placed onto the skb. */726726+void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, 727727+ size_t len)753728{754754- int i;729729+ int i, avail, new_len;730730+ unsigned char *ptr;731731+ struct sk_buff *skb;732732+ static const unsigned char *hex = "0123456789ABCDEF";755733756756- for (i=0; i<len; i++)757757- audit_log_format(ab, "%02x", buf[i]);734734+ BUG_ON(!ab->skb);735735+ skb = ab->skb;736736+ avail = skb_tailroom(skb);737737+ new_len = len<<1;738738+ if (new_len >= avail) {739739+ /* Round the buffer request up to the next multiple */740740+ new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);741741+ avail = audit_expand(ab, new_len);742742+ if (!avail)743743+ return;744744+ }745745+746746+ ptr = skb->tail;747747+ for (i=0; i<len; i++) {748748+ *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */749749+ *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */750750+ }751751+ *ptr = 0;752752+ skb_put(skb, len << 1); /* new string is twice the old string */758753}759754755755+/* This code will escape a string that is passed to it if the string756756+ * contains a control character, unprintable character, double quote mark, 757757+ * or a space. Unescaped strings will start and end with a double quote mark.758758+ * Strings that are escaped are printed in hex (2 digits per char). */760759void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)761760{762761 const unsigned char *p = string;763762764763 while (*p) {765765- if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {764764+ if (*p == '"' || *p < 0x21 || *p > 0x7f) {766765 audit_log_hex(ab, string, strlen(string));767766 return;768767 }···798743 audit_log_format(ab, "\"%s\"", string);799744}800745801801-802802-/* This is a helper-function to print the d_path without using a static803803- * buffer or allocating another buffer in addition to the one in804804- * audit_buffer. */746746+/* This is a helper-function to print the escaped d_path */805747void audit_log_d_path(struct audit_buffer *ab, const char *prefix,806748 struct dentry *dentry, struct vfsmount *vfsmnt)807749{808808- char *p;809809- int len, avail;750750+ char *p, *path;810751811811- if (prefix) audit_log_format(ab, " %s", prefix);752752+ if (prefix)753753+ audit_log_format(ab, " %s", prefix);812754813813- if (ab->len > 128)814814- audit_log_move(ab);815815- avail = sizeof(ab->tmp) - ab->len;816816- p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);817817- if (IS_ERR(p)) {755755+ /* We will allow 11 spaces for ' (deleted)' to be appended */756756+ path = kmalloc(PATH_MAX+11, GFP_KERNEL);757757+ if (!path) {758758+ audit_log_format(ab, "<no memory>");759759+ return;760760+ }761761+ p = d_path(dentry, vfsmnt, path, PATH_MAX+11);762762+ if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */818763 /* FIXME: can we save some information here? */819819- audit_log_format(ab, "<toolong>");820820- } else {821821- /* path isn't at start of buffer */822822- len = (ab->tmp + sizeof(ab->tmp) - 1) - p;823823- memmove(ab->tmp + ab->len, p, len);824824- ab->len += len;825825- ab->total += len;826826- }764764+ audit_log_format(ab, "<too long>");765765+ } else 766766+ audit_log_untrustedstring(ab, p);767767+ kfree(path);827768}828828-829829-/* Remove queued messages from the audit_txlist and send them to userspace. */830830-static void audit_tasklet_handler(unsigned long arg)831831-{832832- LIST_HEAD(list);833833- struct audit_buffer *ab;834834- unsigned long flags;835835-836836- spin_lock_irqsave(&audit_txlist_lock, flags);837837- list_splice_init(&audit_txlist, &list);838838- spin_unlock_irqrestore(&audit_txlist_lock, flags);839839-840840- while (!list_empty(&list)) {841841- ab = list_entry(list.next, struct audit_buffer, list);842842- list_del(&ab->list);843843- audit_log_end_fast(ab);844844- }845845-}846846-847847-static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);848769849770/* The netlink_* functions cannot be called inside an irq context, so850771 * the audit buffer is places on a queue and a tasklet is scheduled to851772 * remove them from the queue outside the irq context. May be called in852773 * any context. */853853-static void audit_log_end_irq(struct audit_buffer *ab)774774+void audit_log_end(struct audit_buffer *ab)854775{855855- unsigned long flags;856856-857857- if (!ab)858858- return;859859- spin_lock_irqsave(&audit_txlist_lock, flags);860860- list_add_tail(&ab->list, &audit_txlist);861861- spin_unlock_irqrestore(&audit_txlist_lock, flags);862862-863863- tasklet_schedule(&audit_tasklet);864864-}865865-866866-/* Send the message in the audit buffer directly to user space. May not867867- * be called in an irq context. */868868-static void audit_log_end_fast(struct audit_buffer *ab)869869-{870870- unsigned long flags;871871-872872- BUG_ON(in_irq());873776 if (!ab)874777 return;875778 if (!audit_rate_check()) {876779 audit_log_lost("rate limit exceeded");877780 } else {878878- audit_log_move(ab);879879- if (audit_log_drain(ab))880880- return;781781+ if (audit_pid) {782782+ struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;783783+ nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);784784+ skb_queue_tail(&audit_skb_queue, ab->skb);785785+ ab->skb = NULL;786786+ wake_up_interruptible(&kauditd_wait);787787+ } else {788788+ printk("%s\n", ab->skb->data + NLMSG_SPACE(0));789789+ }881790 }882882-883883- atomic_dec(&audit_backlog);884884- spin_lock_irqsave(&audit_freelist_lock, flags);885885- if (++audit_freelist_count > AUDIT_MAXFREE)886886- kfree(ab);887887- else888888- list_add(&ab->list, &audit_freelist);889889- spin_unlock_irqrestore(&audit_freelist_lock, flags);890890-}891891-892892-/* Send or queue the message in the audit buffer, depending on the893893- * current context. (A convenience function that may be called in any894894- * context.) */895895-void audit_log_end(struct audit_buffer *ab)896896-{897897- if (in_irq())898898- audit_log_end_irq(ab);899899- else900900- audit_log_end_fast(ab);791791+ audit_buffer_free(ab);901792}902793903794/* Log an audit record. This is a convenience function that calls904795 * audit_log_start, audit_log_vformat, and audit_log_end. It may be905796 * called in any context. */906906-void audit_log(struct audit_context *ctx, const char *fmt, ...)797797+void audit_log(struct audit_context *ctx, int type, const char *fmt, ...)907798{908799 struct audit_buffer *ab;909800 va_list args;910801911911- ab = audit_log_start(ctx);802802+ ab = audit_log_start(ctx, type);912803 if (ab) {913804 va_start(args, fmt);914805 audit_log_vformat(ab, fmt, args);
+184-75
kernel/auditsc.c
···3434#include <asm/types.h>3535#include <linux/mm.h>3636#include <linux/module.h>3737-3737+#include <linux/mount.h>3838+#include <linux/socket.h>3839#include <linux/audit.h>3940#include <linux/personality.h>4041#include <linux/time.h>···113112 mode_t mode;114113};115114115115+struct audit_aux_data_socketcall {116116+ struct audit_aux_data d;117117+ int nargs;118118+ unsigned long args[0];119119+};120120+121121+struct audit_aux_data_sockaddr {122122+ struct audit_aux_data d;123123+ int len;124124+ char a[0];125125+};126126+127127+struct audit_aux_data_path {128128+ struct audit_aux_data d;129129+ struct dentry *dentry;130130+ struct vfsmount *mnt;131131+};116132117133/* The per-task audit context. */118134struct audit_context {···145127 int auditable; /* 1 if record should be written */146128 int name_count;147129 struct audit_names names[AUDIT_NAMES];130130+ struct dentry * pwd;131131+ struct vfsmount * pwdmnt;148132 struct audit_context *previous; /* For nested syscalls */149133 struct audit_aux_data *aux;150134···176156 struct rcu_head rcu;177157 struct audit_rule rule;178158};159159+160160+extern int audit_pid;179161180162/* Check to see if two rules are identical. It is called from181163 * audit_del_rule during AUDIT_DEL. */···248226 return -EFAULT; /* No matching rule */249227}250228251251-#ifdef CONFIG_NET252229/* Copy rule from user-space to kernel-space. Called during253230 * AUDIT_ADD. */254231static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)···308287 err = audit_add_rule(entry, &audit_entlist);309288 if (!err && (flags & AUDIT_AT_EXIT))310289 err = audit_add_rule(entry, &audit_extlist);311311- audit_log(NULL, "auid %u added an audit rule\n", loginuid);290290+ audit_log(NULL, AUDIT_CONFIG_CHANGE, 291291+ "auid=%u added an audit rule\n", loginuid);312292 break;313293 case AUDIT_DEL:314294 flags =((struct audit_rule *)data)->flags;···319297 err = audit_del_rule(data, &audit_entlist);320298 if (!err && (flags & AUDIT_AT_EXIT))321299 err = audit_del_rule(data, &audit_extlist);322322- audit_log(NULL, "auid %u removed an audit rule\n", loginuid);300300+ audit_log(NULL, AUDIT_CONFIG_CHANGE,301301+ "auid=%u removed an audit rule\n", loginuid);323302 break;324303 default:325304 return -EINVAL;···328305329306 return err;330307}331331-#endif332308333309/* Compare a task_struct with an audit_rule. Return 1 on match, 0334310 * otherwise. */···466444467445/* At syscall entry and exit time, this filter is called if the468446 * audit_state is not low enough that auditing cannot take place, but is469469- * also not high enough that we already know we have to write and audit447447+ * also not high enough that we already know we have to write an audit470448 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).471449 */472450static enum audit_state audit_filter_syscall(struct task_struct *tsk,···554532 if (context->names[i].name)555533 __putname(context->names[i].name);556534 context->name_count = 0;535535+ if (context->pwd)536536+ dput(context->pwd);537537+ if (context->pwdmnt)538538+ mntput(context->pwdmnt);539539+ context->pwd = NULL;540540+ context->pwdmnt = NULL;557541}558542559543static inline void audit_free_aux(struct audit_context *context)···567539 struct audit_aux_data *aux;568540569541 while ((aux = context->aux)) {542542+ if (aux->type == AUDIT_AVC_PATH) {543543+ struct audit_aux_data_path *axi = (void *)aux;544544+ dput(axi->dentry);545545+ mntput(axi->mnt);546546+ }570547 context->aux = aux->next;571548 kfree(aux);572549 }···658625 struct vm_area_struct *vma;659626660627 get_task_comm(name, current);661661- audit_log_format(ab, " comm=%s", name);628628+ audit_log_format(ab, " comm=");629629+ audit_log_untrustedstring(ab, name);662630663631 if (!mm)664632 return;···683649{684650 int i;685651 struct audit_buffer *ab;652652+ struct audit_aux_data *aux;686653687687- ab = audit_log_start(context);654654+ ab = audit_log_start(context, AUDIT_SYSCALL);688655 if (!ab)689656 return; /* audit_panic has been called */690690- audit_log_format(ab, "syscall=%d", context->major);657657+ audit_log_format(ab, "arch=%x syscall=%d",658658+ context->arch, context->major);691659 if (context->personality != PER_LINUX)692660 audit_log_format(ab, " per=%lx", context->personality);693693- audit_log_format(ab, " arch=%x", context->arch);694661 if (context->return_valid)695662 audit_log_format(ab, " success=%s exit=%ld", 696663 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",697664 context->return_code);698665 audit_log_format(ab,699666 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"700700- " pid=%d loginuid=%d uid=%d gid=%d"701701- " euid=%d suid=%d fsuid=%d"702702- " egid=%d sgid=%d fsgid=%d",667667+ " pid=%d auid=%u uid=%u gid=%u"668668+ " euid=%u suid=%u fsuid=%u"669669+ " egid=%u sgid=%u fsgid=%u",703670 context->argv[0],704671 context->argv[1],705672 context->argv[2],···714679 context->egid, context->sgid, context->fsgid);715680 audit_log_task_info(ab);716681 audit_log_end(ab);717717- while (context->aux) {718718- struct audit_aux_data *aux;719682720720- ab = audit_log_start(context);683683+ for (aux = context->aux; aux; aux = aux->next) {684684+685685+ ab = audit_log_start(context, aux->type);721686 if (!ab)722687 continue; /* audit_panic has been called */723688724724- aux = context->aux;725725- context->aux = aux->next;726726-727727- audit_log_format(ab, "auxitem=%d", aux->type);728689 switch (aux->type) {729729- case AUDIT_AUX_IPCPERM: {690690+ case AUDIT_IPC: {730691 struct audit_aux_data_ipcctl *axi = (void *)aux;731692 audit_log_format(ab, 732732- " qbytes=%lx uid=%d gid=%d mode=%x",693693+ " qbytes=%lx iuid=%u igid=%u mode=%x",733694 axi->qbytes, axi->uid, axi->gid, axi->mode);734734- }695695+ break; }696696+697697+ case AUDIT_SOCKETCALL: {698698+ int i;699699+ struct audit_aux_data_socketcall *axs = (void *)aux;700700+ audit_log_format(ab, "nargs=%d", axs->nargs);701701+ for (i=0; i<axs->nargs; i++)702702+ audit_log_format(ab, " a%d=%lx", i, axs->args[i]);703703+ break; }704704+705705+ case AUDIT_SOCKADDR: {706706+ struct audit_aux_data_sockaddr *axs = (void *)aux;707707+708708+ audit_log_format(ab, "saddr=");709709+ audit_log_hex(ab, axs->a, axs->len);710710+ break; }711711+712712+ case AUDIT_AVC_PATH: {713713+ struct audit_aux_data_path *axi = (void *)aux;714714+ audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);715715+ break; }716716+735717 }736718 audit_log_end(ab);737737- kfree(aux);738719 }739720721721+ if (context->pwd && context->pwdmnt) {722722+ ab = audit_log_start(context, AUDIT_CWD);723723+ if (ab) {724724+ audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);725725+ audit_log_end(ab);726726+ }727727+ }740728 for (i = 0; i < context->name_count; i++) {741741- ab = audit_log_start(context);729729+ ab = audit_log_start(context, AUDIT_PATH);742730 if (!ab)743731 continue; /* audit_panic has been called */732732+744733 audit_log_format(ab, "item=%d", i);745734 if (context->names[i].name) {746735 audit_log_format(ab, " name=");···772713 }773714 if (context->names[i].ino != (unsigned long)-1)774715 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"775775- " uid=%d gid=%d rdev=%02x:%02x",716716+ " ouid=%u ogid=%u rdev=%02x:%02x",776717 context->names[i].ino,777718 MAJOR(context->names[i].dev),778719 MINOR(context->names[i].dev),···800741801742 /* Check for system calls that do not go through the exit802743 * function (e.g., exit_group), then free context block. */803803- if (context->in_syscall && context->auditable)744744+ if (context->in_syscall && context->auditable && context->pid != audit_pid)804745 audit_log_exit(context);805746806747 audit_free_context(context);807807-}808808-809809-/* Compute a serial number for the audit record. Audit records are810810- * written to user-space as soon as they are generated, so a complete811811- * audit record may be written in several pieces. The timestamp of the812812- * record and this serial number are used by the user-space daemon to813813- * determine which pieces belong to the same audit record. The814814- * (timestamp,serial) tuple is unique for each syscall and is live from815815- * syscall entry to syscall exit.816816- *817817- * Atomic values are only guaranteed to be 24-bit, so we count down.818818- *819819- * NOTE: Another possibility is to store the formatted records off the820820- * audit context (for those records that have a context), and emit them821821- * all at syscall exit. However, this could delay the reporting of822822- * significant errors until syscall exit (or never, if the system823823- * halts). */824824-static inline unsigned int audit_serial(void)825825-{826826- static atomic_t serial = ATOMIC_INIT(0xffffff);827827- unsigned int a, b;828828-829829- do {830830- a = atomic_read(&serial);831831- if (atomic_dec_and_test(&serial))832832- atomic_set(&serial, 0xffffff);833833- b = atomic_read(&serial);834834- } while (b != a - 1);835835-836836- return 0xffffff - b;837748}838749839750/* Fill in audit context at syscall entry. This only happens if the···905876 if (likely(!context))906877 return;907878908908- if (context->in_syscall && context->auditable)879879+ if (context->in_syscall && context->auditable && context->pid != audit_pid)909880 audit_log_exit(context);910881911882 context->in_syscall = 0;···945916 context->names[context->name_count].name = name;946917 context->names[context->name_count].ino = (unsigned long)-1;947918 ++context->name_count;919919+ if (!context->pwd) {920920+ read_lock(¤t->fs->lock);921921+ context->pwd = dget(current->fs->pwd);922922+ context->pwdmnt = mntget(current->fs->pwdmnt);923923+ read_unlock(¤t->fs->lock);924924+ }925925+948926}949927950928/* Intercept a putname request. Called from···1030994 context->names[idx].rdev = inode->i_rdev;1031995}103299610331033-void audit_get_stamp(struct audit_context *ctx,10341034- struct timespec *t, unsigned int *serial)997997+void auditsc_get_stamp(struct audit_context *ctx,998998+ struct timespec *t, unsigned int *serial)1035999{10361036- if (ctx) {10371037- t->tv_sec = ctx->ctime.tv_sec;10381038- t->tv_nsec = ctx->ctime.tv_nsec;10391039- *serial = ctx->serial;10401040- ctx->auditable = 1;10411041- } else {10421042- *t = CURRENT_TIME;10431043- *serial = 0;10441044- }10001000+ t->tv_sec = ctx->ctime.tv_sec;10011001+ t->tv_nsec = ctx->ctime.tv_nsec;10021002+ *serial = ctx->serial;10031003+ ctx->auditable = 1;10451004}10461046-10471047-extern int audit_set_type(struct audit_buffer *ab, int type);1048100510491006int audit_set_loginuid(struct task_struct *task, uid_t loginuid)10501007{10511008 if (task->audit_context) {10521009 struct audit_buffer *ab;1053101010541054- ab = audit_log_start(NULL);10111011+ ab = audit_log_start(NULL, AUDIT_LOGIN);10551012 if (ab) {10561013 audit_log_format(ab, "login pid=%d uid=%u "10571057- "old loginuid=%u new loginuid=%u",10141014+ "old auid=%u new auid=%u",10581015 task->pid, task->uid, 10591016 task->audit_context->loginuid, loginuid);10601060- audit_set_type(ab, AUDIT_LOGIN);10611017 audit_log_end(ab);10621018 }10631019 task->audit_context->loginuid = loginuid;···10791051 ax->gid = gid;10801052 ax->mode = mode;1081105310821082- ax->d.type = AUDIT_AUX_IPCPERM;10541054+ ax->d.type = AUDIT_IPC;10831055 ax->d.next = context->aux;10841056 context->aux = (void *)ax;10851057 return 0;10861058}10591059+10601060+int audit_socketcall(int nargs, unsigned long *args)10611061+{10621062+ struct audit_aux_data_socketcall *ax;10631063+ struct audit_context *context = current->audit_context;10641064+10651065+ if (likely(!context))10661066+ return 0;10671067+10681068+ ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);10691069+ if (!ax)10701070+ return -ENOMEM;10711071+10721072+ ax->nargs = nargs;10731073+ memcpy(ax->args, args, nargs * sizeof(unsigned long));10741074+10751075+ ax->d.type = AUDIT_SOCKETCALL;10761076+ ax->d.next = context->aux;10771077+ context->aux = (void *)ax;10781078+ return 0;10791079+}10801080+10811081+int audit_sockaddr(int len, void *a)10821082+{10831083+ struct audit_aux_data_sockaddr *ax;10841084+ struct audit_context *context = current->audit_context;10851085+10861086+ if (likely(!context))10871087+ return 0;10881088+10891089+ ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);10901090+ if (!ax)10911091+ return -ENOMEM;10921092+10931093+ ax->len = len;10941094+ memcpy(ax->a, a, len);10951095+10961096+ ax->d.type = AUDIT_SOCKADDR;10971097+ ax->d.next = context->aux;10981098+ context->aux = (void *)ax;10991099+ return 0;11001100+}11011101+11021102+int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)11031103+{11041104+ struct audit_aux_data_path *ax;11051105+ struct audit_context *context = current->audit_context;11061106+11071107+ if (likely(!context))11081108+ return 0;11091109+11101110+ ax = kmalloc(sizeof(*ax), GFP_ATOMIC);11111111+ if (!ax)11121112+ return -ENOMEM;11131113+11141114+ ax->dentry = dget(dentry);11151115+ ax->mnt = mntget(mnt);11161116+11171117+ ax->d.type = AUDIT_AVC_PATH;11181118+ ax->d.next = context->aux;11191119+ context->aux = (void *)ax;11201120+ return 0;11211121+}11221122+11231123+void audit_signal_info(int sig, struct task_struct *t)11241124+{11251125+ extern pid_t audit_sig_pid;11261126+ extern uid_t audit_sig_uid;11271127+11281128+ if (unlikely(audit_pid && t->pid == audit_pid)) {11291129+ if (sig == SIGTERM || sig == SIGHUP) {11301130+ struct audit_context *ctx = current->audit_context;11311131+ audit_sig_pid = current->pid;11321132+ if (ctx)11331133+ audit_sig_uid = ctx->loginuid;11341134+ else11351135+ audit_sig_uid = current->uid;11361136+ }11371137+ }11381138+}11391139+
+6-1
kernel/signal.c
···2424#include <linux/ptrace.h>2525#include <linux/posix-timers.h>2626#include <linux/signal.h>2727+#include <linux/audit.h>2728#include <asm/param.h>2829#include <asm/uaccess.h>2930#include <asm/unistd.h>···668667 && (current->uid ^ t->suid) && (current->uid ^ t->uid)669668 && !capable(CAP_KILL))670669 return error;671671- return security_task_kill(t, info, sig);670670+671671+ error = security_task_kill(t, info, sig);672672+ if (!error)673673+ audit_signal_info(sig, t); /* Let audit system see the signal */674674+ return error;672675}673676674677/* forward decl */
+7-2
net/socket.c
···8181#include <linux/syscalls.h>8282#include <linux/compat.h>8383#include <linux/kmod.h>8484+#include <linux/audit.h>84858586#ifdef CONFIG_NET_RADIO8687#include <linux/wireless.h> /* Note : will define WIRELESS_EXT */···227226 return 0;228227 if(copy_from_user(kaddr,uaddr,ulen))229228 return -EFAULT;230230- return 0;229229+ return audit_sockaddr(ulen, kaddr);231230}232231233232/**···19071906 /* copy_from_user should be SMP safe. */19081907 if (copy_from_user(a, args, nargs[call]))19091908 return -EFAULT;19101910-19091909+19101910+ err = audit_socketcall(nargs[call]/sizeof(unsigned long), a);19111911+ if (err)19121912+ return err;19131913+19111914 a0=a[0];19121915 a1=a[1];19131916
+21-19
security/selinux/avc.c
···242242 avc_node_cachep = kmem_cache_create("avc_node", sizeof(struct avc_node),243243 0, SLAB_PANIC, NULL, NULL);244244245245- audit_log(current->audit_context, "AVC INITIALIZED\n");245245+ audit_log(current->audit_context, AUDIT_KERNEL, "AVC INITIALIZED\n");246246}247247248248int avc_get_hash_stats(char *page)···532532 u16 tclass, u32 requested,533533 struct av_decision *avd, int result, struct avc_audit_data *a)534534{535535+ struct task_struct *tsk = current;535536 struct inode *inode = NULL;536537 u32 denied, audited;537538 struct audit_buffer *ab;···550549 return;551550 }552551553553- ab = audit_log_start(current->audit_context);552552+ ab = audit_log_start(current->audit_context, AUDIT_AVC);554553 if (!ab)555554 return; /* audit_panic has been called */556555 audit_log_format(ab, "avc: %s ", denied ? "denied" : "granted");557556 avc_dump_av(ab, tclass,audited);558557 audit_log_format(ab, " for ");558558+ if (a && a->tsk)559559+ tsk = a->tsk;560560+ if (tsk && tsk->pid) {561561+ audit_log_format(ab, " pid=%d comm=", tsk->pid);562562+ audit_log_untrustedstring(ab, tsk->comm);563563+ }559564 if (a) {560565 switch (a->type) {561566 case AVC_AUDIT_DATA_IPC:···573566 case AVC_AUDIT_DATA_FS:574567 if (a->u.fs.dentry) {575568 struct dentry *dentry = a->u.fs.dentry;576576- if (a->u.fs.mnt) {577577- audit_log_d_path(ab, "path=", dentry,578578- a->u.fs.mnt);579579- } else {580580- audit_log_format(ab, " name=%s",581581- dentry->d_name.name);582582- }569569+ if (a->u.fs.mnt)570570+ audit_avc_path(dentry, a->u.fs.mnt);571571+ audit_log_format(ab, " name=");572572+ audit_log_untrustedstring(ab, dentry->d_name.name);583573 inode = dentry->d_inode;584574 } else if (a->u.fs.inode) {585575 struct dentry *dentry;586576 inode = a->u.fs.inode;587577 dentry = d_find_alias(inode);588578 if (dentry) {589589- audit_log_format(ab, " name=%s",590590- dentry->d_name.name);579579+ audit_log_format(ab, " name=");580580+ audit_log_untrustedstring(ab, dentry->d_name.name);591581 dput(dentry);592582 }593583 }···627623 case AF_UNIX:628624 u = unix_sk(sk);629625 if (u->dentry) {630630- audit_log_d_path(ab, "path=",631631- u->dentry, u->mnt);626626+ audit_avc_path(u->dentry, u->mnt);627627+ audit_log_format(ab, " name=");628628+ audit_log_untrustedstring(ab, u->dentry->d_name.name);632629 break;633630 }634631 if (!u->addr)635632 break;636633 len = u->addr->len-sizeof(short);637634 p = &u->addr->name->sun_path[0];635635+ audit_log_format(ab, " path=");638636 if (*p)639639- audit_log_format(ab,640640- "path=%*.*s", len,641641- len, p);637637+ audit_log_untrustedstring(ab, p);642638 else643643- audit_log_format(ab,644644- "path=@%*.*s", len-1,645645- len-1, p+1);639639+ audit_log_hex(ab, p, len);646640 break;647641 }648642 }
+1-1
security/selinux/hooks.c
···34193419 err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm);34203420 if (err) {34213421 if (err == -EINVAL) {34223422- audit_log(current->audit_context,34223422+ audit_log(current->audit_context, AUDIT_SELINUX_ERR,34233423 "SELinux: unrecognized netlink message"34243424 " type=%hu for sclass=%hu\n",34253425 nlh->nlmsg_type, isec->sclass);
+8-2
security/selinux/nlmsgtab.c
···9797 { AUDIT_ADD, NETLINK_AUDIT_SOCKET__NLMSG_WRITE },9898 { AUDIT_DEL, NETLINK_AUDIT_SOCKET__NLMSG_WRITE },9999 { AUDIT_USER, NETLINK_AUDIT_SOCKET__NLMSG_RELAY },100100+ { AUDIT_SIGNAL_INFO, NETLINK_AUDIT_SOCKET__NLMSG_READ },100101};101102102103···142141 break;143142144143 case SECCLASS_NETLINK_AUDIT_SOCKET:145145- err = nlmsg_perm(nlmsg_type, perm, nlmsg_audit_perms,146146- sizeof(nlmsg_audit_perms));144144+ if (nlmsg_type >= AUDIT_FIRST_USER_MSG &&145145+ nlmsg_type <= AUDIT_LAST_USER_MSG) {146146+ *perm = NETLINK_AUDIT_SOCKET__NLMSG_RELAY;147147+ } else {148148+ err = nlmsg_perm(nlmsg_type, perm, nlmsg_audit_perms,149149+ sizeof(nlmsg_audit_perms));150150+ }147151 break;148152149153 /* No messaging from userspace, or class unknown/unhandled */
+2-2
security/selinux/ss/services.c
···365365 goto out;366366 if (context_struct_to_string(tcontext, &t, &tlen) < 0)367367 goto out;368368- audit_log(current->audit_context,368368+ audit_log(current->audit_context, AUDIT_SELINUX_ERR,369369 "security_validate_transition: denied for"370370 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",371371 o, n, t, policydb.p_class_val_to_name[tclass-1]);···742742 goto out;743743 if (context_struct_to_string(newcontext, &n, &nlen) < 0)744744 goto out;745745- audit_log(current->audit_context,745745+ audit_log(current->audit_context, AUDIT_SELINUX_ERR,746746 "security_compute_sid: invalid context %s"747747 " for scontext=%s"748748 " tcontext=%s"