⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audit.c

📁 Kernel code of linux kernel
💻 C
📖 第 1 页 / 共 3 页
字号:
/* audit.c -- Auditing support * Gateway between the kernel (e.g., selinux) and the user-space audit daemon. * System-call specific features have moved to auditsc.c * * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina. * All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * Written by Rickard E. (Rik) Faith <faith@redhat.com> * * Goals: 1) Integrate fully with Security Modules. *	  2) Minimal run-time overhead: *	     a) Minimal when syscall auditing is disabled (audit_enable=0). *	     b) Small when syscall auditing is enabled and no audit record *		is generated (defer as much work as possible to record *		generation time): *		i) context is allocated, *		ii) names from getname are stored without a copy, and *		iii) inode information stored from path_lookup. *	  3) Ability to disable syscall auditing at boot time (audit=0). *	  4) Usable by other parts of the kernel (if audit_log* is called, *	     then a syscall record will be generated automatically for the *	     current syscall). *	  5) Netlink interface to user-space. *	  6) Support low-overhead kernel-based filtering to minimize the *	     information that must be passed to user-space. * * Example user-space utilities: http://people.redhat.com/sgrubb/audit/ */#include <linux/init.h>#include <asm/types.h>#include <asm/atomic.h>#include <linux/mm.h>#include <linux/module.h>#include <linux/err.h>#include <linux/kthread.h>#include <linux/audit.h>#include <net/sock.h>#include <net/netlink.h>#include <linux/skbuff.h>#include <linux/netlink.h>#include <linux/inotify.h>#include <linux/freezer.h>#include <linux/tty.h>#include "audit.h"/* No auditing will take place until audit_initialized != 0. * (Initialization happens after skb_init is called.) */static int	audit_initialized;#define AUDIT_OFF	0#define AUDIT_ON	1#define AUDIT_LOCKED	2int		audit_enabled;int		audit_ever_enabled;/* Default state when kernel boots without any parameters. */static int	audit_default;/* If auditing cannot proceed, audit_failure selects what happens. */static int	audit_failure = AUDIT_FAIL_PRINTK;/* * If audit records are to be written to the netlink socket, audit_pid * contains the pid of the auditd process and audit_nlk_pid contains * the pid to use to send netlink messages to that process. */int		audit_pid;static int	audit_nlk_pid;/* If audit_rate_limit is non-zero, limit the rate of sending audit records * to that number per second.  This prevents DoS attacks, but results in * audit records being dropped. */static int	audit_rate_limit;/* Number of outstanding audit_buffers allowed. */static int	audit_backlog_limit = 64;static int	audit_backlog_wait_time = 60 * HZ;static int	audit_backlog_wait_overflow = 0;/* The identity of the user shutting down the audit system. */uid_t		audit_sig_uid = -1;pid_t		audit_sig_pid = -1;u32		audit_sig_sid = 0;/* Records can be lost in several ways:   0) [suppressed in audit_alloc]   1) out of memory in audit_log_start [kmalloc of struct audit_buffer]   2) out of memory in audit_log_move [alloc_skb]   3) suppressed due to audit_rate_limit   4) suppressed due to audit_backlog_limit*/static atomic_t    audit_lost = ATOMIC_INIT(0);/* The netlink socket. */static struct sock *audit_sock;/* Inotify handle. */struct inotify_handle *audit_ih;/* Hash for inode-based rules */struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];/* The audit_freelist is a list of pre-allocated audit buffers (if more * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of * being placed on the freelist). */static DEFINE_SPINLOCK(audit_freelist_lock);static int	   audit_freelist_count;static LIST_HEAD(audit_freelist);static struct sk_buff_head audit_skb_queue;/* queue of skbs to send to auditd when/if it comes back */static struct sk_buff_head audit_skb_hold_queue;static struct task_struct *kauditd_task;static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);/* Serialize requests from userspace. */static DEFINE_MUTEX(audit_cmd_mutex);/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting * audit records.  Since printk uses a 1024 byte buffer, this buffer * should be at least that large. */#define AUDIT_BUFSIZ 1024/* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the * audit_freelist.  Doing so eliminates many kmalloc/kfree calls. */#define AUDIT_MAXFREE  (2*NR_CPUS)/* The audit_buffer is used when formatting an audit record.  The caller * locks briefly to get the record off the freelist or to allocate the * buffer, and locks briefly to send the buffer to the netlink layer or * to place it on a transmit queue.  Multiple audit_buffers can be in * use simultaneously. */struct audit_buffer {	struct list_head     list;	struct sk_buff       *skb;	/* formatted skb ready to send */	struct audit_context *ctx;	/* NULL or associated context */	gfp_t		     gfp_mask;};struct audit_reply {	int pid;	struct sk_buff *skb;};static void audit_set_pid(struct audit_buffer *ab, pid_t pid){	if (ab) {		struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);		nlh->nlmsg_pid = pid;	}}void audit_panic(const char *message){	switch (audit_failure)	{	case AUDIT_FAIL_SILENT:		break;	case AUDIT_FAIL_PRINTK:		if (printk_ratelimit())			printk(KERN_ERR "audit: %s\n", message);		break;	case AUDIT_FAIL_PANIC:		/* test audit_pid since printk is always losey, why bother? */		if (audit_pid)			panic("audit: %s\n", message);		break;	}}static inline int audit_rate_check(void){	static unsigned long	last_check = 0;	static int		messages   = 0;	static DEFINE_SPINLOCK(lock);	unsigned long		flags;	unsigned long		now;	unsigned long		elapsed;	int			retval	   = 0;	if (!audit_rate_limit) return 1;	spin_lock_irqsave(&lock, flags);	if (++messages < audit_rate_limit) {		retval = 1;	} else {		now     = jiffies;		elapsed = now - last_check;		if (elapsed > HZ) {			last_check = now;			messages   = 0;			retval     = 1;		}	}	spin_unlock_irqrestore(&lock, flags);	return retval;}/** * audit_log_lost - conditionally log lost audit message event * @message: the message stating reason for lost audit message * * Emit at least 1 message per second, even if audit_rate_check is * throttling. * Always increment the lost messages counter.*/void audit_log_lost(const char *message){	static unsigned long	last_msg = 0;	static DEFINE_SPINLOCK(lock);	unsigned long		flags;	unsigned long		now;	int			print;	atomic_inc(&audit_lost);	print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);	if (!print) {		spin_lock_irqsave(&lock, flags);		now = jiffies;		if (now - last_msg > HZ) {			print = 1;			last_msg = now;		}		spin_unlock_irqrestore(&lock, flags);	}	if (print) {		if (printk_ratelimit())			printk(KERN_WARNING				"audit: audit_lost=%d audit_rate_limit=%d "				"audit_backlog_limit=%d\n",				atomic_read(&audit_lost),				audit_rate_limit,				audit_backlog_limit);		audit_panic(message);	}}static int audit_log_config_change(char *function_name, int new, int old,				   uid_t loginuid, u32 sessionid, u32 sid,				   int allow_changes){	struct audit_buffer *ab;	int rc = 0;	ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);	audit_log_format(ab, "%s=%d old=%d auid=%u ses=%u", function_name, new,			 old, loginuid, sessionid);	if (sid) {		char *ctx = NULL;		u32 len;		rc = security_secid_to_secctx(sid, &ctx, &len);		if (rc) {			audit_log_format(ab, " sid=%u", sid);			allow_changes = 0; /* Something weird, deny request */		} else {			audit_log_format(ab, " subj=%s", ctx);			security_release_secctx(ctx, len);		}	}	audit_log_format(ab, " res=%d", allow_changes);	audit_log_end(ab);	return rc;}static int audit_do_config_change(char *function_name, int *to_change,				  int new, uid_t loginuid, u32 sessionid,				  u32 sid){	int allow_changes, rc = 0, old = *to_change;	/* check if we are locked */	if (audit_enabled == AUDIT_LOCKED)		allow_changes = 0;	else		allow_changes = 1;	if (audit_enabled != AUDIT_OFF) {		rc = audit_log_config_change(function_name, new, old, loginuid,					     sessionid, sid, allow_changes);		if (rc)			allow_changes = 0;	}	/* If we are allowed, make the change */	if (allow_changes == 1)		*to_change = new;	/* Not allowed, update reason */	else if (rc == 0)		rc = -EPERM;	return rc;}static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sessionid,				u32 sid){	return audit_do_config_change("audit_rate_limit", &audit_rate_limit,				      limit, loginuid, sessionid, sid);}static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sessionid,				   u32 sid){	return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit,				      limit, loginuid, sessionid, sid);}static int audit_set_enabled(int state, uid_t loginuid, u32 sessionid, u32 sid){	int rc;	if (state < AUDIT_OFF || state > AUDIT_LOCKED)		return -EINVAL;	rc =  audit_do_config_change("audit_enabled", &audit_enabled, state,				     loginuid, sessionid, sid);	if (!rc)		audit_ever_enabled |= !!state;	return rc;}static int audit_set_failure(int state, uid_t loginuid, u32 sessionid, u32 sid){	if (state != AUDIT_FAIL_SILENT	    && state != AUDIT_FAIL_PRINTK	    && state != AUDIT_FAIL_PANIC)		return -EINVAL;	return audit_do_config_change("audit_failure", &audit_failure, state,				      loginuid, sessionid, sid);}/* * Queue skbs to be sent to auditd when/if it comes back.  These skbs should * already have been sent via prink/syslog and so if these messages are dropped * it is not a huge concern since we already passed the audit_log_lost() * notification and stuff.  This is just nice to get audit messages during * boot before auditd is running or messages generated while auditd is stopped. * This only holds messages is audit_default is set, aka booting with audit=1 * or building your kernel that way. */static void audit_hold_skb(struct sk_buff *skb){	if (audit_default &&	    skb_queue_len(&audit_skb_hold_queue) < audit_backlog_limit)		skb_queue_tail(&audit_skb_hold_queue, skb);	else		kfree_skb(skb);}static void kauditd_send_skb(struct sk_buff *skb){	int err;	/* take a reference in case we can't send it and we want to hold it */	skb_get(skb);	err = netlink_unicast(audit_sock, skb, audit_nlk_pid, 0);	if (err < 0) {		BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */		printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);		audit_log_lost("auditd dissapeared\n");		audit_pid = 0;		/* we might get lucky and get this in the next auditd */		audit_hold_skb(skb);	} else		/* drop the extra reference if sent ok */		kfree_skb(skb);}static int kauditd_thread(void *dummy){	struct sk_buff *skb;	set_freezable();	while (!kthread_should_stop()) {		/*		 * if auditd just started drain the queue of messages already		 * sent to syslog/printk.  remember loss here is ok.  we already		 * called audit_log_lost() if it didn't go out normally.  so the		 * race between the skb_dequeue and the next check for audit_pid		 * doesn't matter.		 *		 * if you ever find kauditd to be too slow we can get a perf win		 * by doing our own locking and keeping better track if there		 * are messages in this queue.  I don't see the need now, but		 * in 5 years when I want to play with this again I'll see this		 * note and still have no friggin idea what i'm thinking today.		 */		if (audit_default && audit_pid) {			skb = skb_dequeue(&audit_skb_hold_queue);			if (unlikely(skb)) {				while (skb && audit_pid) {					kauditd_send_skb(skb);					skb = skb_dequeue(&audit_skb_hold_queue);				}			}		}		skb = skb_dequeue(&audit_skb_queue);		wake_up(&audit_backlog_wait);		if (skb) {			if (audit_pid)				kauditd_send_skb(skb);			else {				if (printk_ratelimit())					printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));				else					audit_log_lost("printk limit exceeded\n");				audit_hold_skb(skb);			}		} else {			DECLARE_WAITQUEUE(wait, current);			set_current_state(TASK_INTERRUPTIBLE);			add_wait_queue(&kauditd_wait, &wait);			if (!skb_queue_len(&audit_skb_queue)) {				try_to_freeze();				schedule();			}			__set_current_state(TASK_RUNNING);			remove_wait_queue(&kauditd_wait, &wait);		}	}	return 0;}static int audit_prepare_user_tty(pid_t pid, uid_t loginuid, u32 sessionid){	struct task_struct *tsk;	int err;	read_lock(&tasklist_lock);	tsk = find_task_by_vpid(pid);	err = -ESRCH;	if (!tsk)		goto out;	err = 0;	spin_lock_irq(&tsk->sighand->siglock);	if (!tsk->signal->audit_tty)		err = -EPERM;	spin_unlock_irq(&tsk->sighand->siglock);	if (err)		goto out;	tty_audit_push_task(tsk, loginuid, sessionid);out:	read_unlock(&tasklist_lock);	return err;}int audit_send_list(void *_dest){	struct audit_netlink_list *dest = _dest;	int pid = dest->pid;	struct sk_buff *skb;	/* wait for parent to finish and send an ACK */	mutex_lock(&audit_cmd_mutex);	mutex_unlock(&audit_cmd_mutex);	while ((skb = __skb_dequeue(&dest->q)) != NULL)		netlink_unicast(audit_sock, skb, pid, 0);	kfree(dest);	return 0;}#ifdef CONFIG_AUDIT_TREEstatic int prune_tree_thread(void *unused){	mutex_lock(&audit_cmd_mutex);	audit_prune_trees();	mutex_unlock(&audit_cmd_mutex);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -