Skip to content

Commit b6c7c11

Browse files
committed
audit: store the auditd PID as a pid struct instead of pid_t
This is arguably the right thing to do, and will make it easier when we start supporting multiple audit daemons in different namespaces. Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 45a0642 commit b6c7c11

2 files changed

Lines changed: 58 additions & 28 deletions

File tree

‎kernel/audit.c‎

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <linux/rcupdate.h>
5959
#include <linux/mutex.h>
6060
#include <linux/gfp.h>
61+
#include <linux/pid.h>
6162

6263
#include <linux/audit.h>
6364

@@ -117,7 +118,7 @@ struct audit_net {
117118
* or the included spinlock for writing.
118119
*/
119120
static struct auditd_connection {
120-
int pid;
121+
struct pid *pid;
121122
u32 portid;
122123
struct net *net;
123124
spinlock_t lock;
@@ -220,17 +221,40 @@ struct audit_reply {
220221
* Description:
221222
* Return 1 if the task is a registered audit daemon, 0 otherwise.
222223
*/
223-
int auditd_test_task(const struct task_struct *task)
224+
int auditd_test_task(struct task_struct *task)
224225
{
225226
int rc;
226227

227228
rcu_read_lock();
228-
rc = (auditd_conn.pid && task->tgid == auditd_conn.pid ? 1 : 0);
229+
rc = (auditd_conn.pid && auditd_conn.pid == task_tgid(task) ? 1 : 0);
229230
rcu_read_unlock();
230231

231232
return rc;
232233
}
233234

235+
/**
236+
* auditd_pid_vnr - Return the auditd PID relative to the namespace
237+
* @auditd: the auditd connection
238+
*
239+
* Description:
240+
* Returns the PID in relation to the namespace, 0 on failure. This function
241+
* takes the RCU read lock internally, but if the caller needs to protect the
242+
* auditd_connection pointer it should take the RCU read lock as well.
243+
*/
244+
static pid_t auditd_pid_vnr(const struct auditd_connection *auditd)
245+
{
246+
pid_t pid;
247+
248+
rcu_read_lock();
249+
if (!auditd || !auditd->pid)
250+
pid = 0;
251+
else
252+
pid = pid_vnr(auditd->pid);
253+
rcu_read_unlock();
254+
255+
return pid;
256+
}
257+
234258
/**
235259
* audit_get_sk - Return the audit socket for the given network namespace
236260
* @net: the destination network namespace
@@ -428,12 +452,17 @@ static int audit_set_failure(u32 state)
428452
* This function will obtain and drop network namespace references as
429453
* necessary.
430454
*/
431-
static void auditd_set(int pid, u32 portid, struct net *net)
455+
static void auditd_set(struct pid *pid, u32 portid, struct net *net)
432456
{
433457
unsigned long flags;
434458

435459
spin_lock_irqsave(&auditd_conn.lock, flags);
436-
auditd_conn.pid = pid;
460+
if (auditd_conn.pid)
461+
put_pid(auditd_conn.pid);
462+
if (pid)
463+
auditd_conn.pid = get_pid(pid);
464+
else
465+
auditd_conn.pid = NULL;
437466
auditd_conn.portid = portid;
438467
if (auditd_conn.net)
439468
put_net(auditd_conn.net);
@@ -1059,11 +1088,13 @@ static int audit_set_feature(struct sk_buff *skb)
10591088
return 0;
10601089
}
10611090

1062-
static int audit_replace(pid_t pid)
1091+
static int audit_replace(struct pid *pid)
10631092
{
1093+
pid_t pvnr;
10641094
struct sk_buff *skb;
10651095

1066-
skb = audit_make_reply(0, AUDIT_REPLACE, 0, 0, &pid, sizeof(pid));
1096+
pvnr = pid_vnr(pid);
1097+
skb = audit_make_reply(0, AUDIT_REPLACE, 0, 0, &pvnr, sizeof(pvnr));
10671098
if (!skb)
10681099
return -ENOMEM;
10691100
return auditd_send_unicast_skb(skb);
@@ -1093,9 +1124,9 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
10931124
memset(&s, 0, sizeof(s));
10941125
s.enabled = audit_enabled;
10951126
s.failure = audit_failure;
1096-
rcu_read_lock();
1097-
s.pid = auditd_conn.pid;
1098-
rcu_read_unlock();
1127+
/* NOTE: use pid_vnr() so the PID is relative to the current
1128+
* namespace */
1129+
s.pid = auditd_pid_vnr(&auditd_conn);
10991130
s.rate_limit = audit_rate_limit;
11001131
s.backlog_limit = audit_backlog_limit;
11011132
s.lost = atomic_read(&audit_lost);
@@ -1121,45 +1152,44 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
11211152
return err;
11221153
}
11231154
if (s.mask & AUDIT_STATUS_PID) {
1124-
/* NOTE: we are using task_tgid_vnr() below because
1125-
* the s.pid value is relative to the namespace
1126-
* of the caller; at present this doesn't matter
1127-
* much since you can really only run auditd
1128-
* from the initial pid namespace, but something
1129-
* to keep in mind if this changes */
1130-
int new_pid = s.pid;
1155+
/* NOTE: we are using the vnr PID functions below
1156+
* because the s.pid value is relative to the
1157+
* namespace of the caller; at present this
1158+
* doesn't matter much since you can really only
1159+
* run auditd from the initial pid namespace, but
1160+
* something to keep in mind if this changes */
1161+
pid_t new_pid = s.pid;
11311162
pid_t auditd_pid;
1132-
pid_t requesting_pid = task_tgid_vnr(current);
1163+
struct pid *req_pid = task_tgid(current);
1164+
1165+
/* sanity check - PID values must match */
1166+
if (new_pid != pid_vnr(req_pid))
1167+
return -EINVAL;
11331168

11341169
/* test the auditd connection */
1135-
audit_replace(requesting_pid);
1170+
audit_replace(req_pid);
11361171

1137-
rcu_read_lock();
1138-
auditd_pid = auditd_conn.pid;
1172+
auditd_pid = auditd_pid_vnr(&auditd_conn);
11391173
/* only the current auditd can unregister itself */
1140-
if ((!new_pid) && (requesting_pid != auditd_pid)) {
1141-
rcu_read_unlock();
1174+
if ((!new_pid) && (new_pid != auditd_pid)) {
11421175
audit_log_config_change("audit_pid", new_pid,
11431176
auditd_pid, 0);
11441177
return -EACCES;
11451178
}
11461179
/* replacing a healthy auditd is not allowed */
11471180
if (auditd_pid && new_pid) {
1148-
rcu_read_unlock();
11491181
audit_log_config_change("audit_pid", new_pid,
11501182
auditd_pid, 0);
11511183
return -EEXIST;
11521184
}
1153-
rcu_read_unlock();
11541185

11551186
if (audit_enabled != AUDIT_OFF)
11561187
audit_log_config_change("audit_pid", new_pid,
11571188
auditd_pid, 1);
11581189

11591190
if (new_pid) {
11601191
/* register a new auditd connection */
1161-
auditd_set(new_pid,
1162-
NETLINK_CB(skb).portid,
1192+
auditd_set(req_pid, NETLINK_CB(skb).portid,
11631193
sock_net(NETLINK_CB(skb).sk));
11641194
/* try to process any backlog */
11651195
wake_up_interruptible(&kauditd_wait);

‎kernel/audit.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ extern void audit_log_name(struct audit_context *context,
218218
struct audit_names *n, const struct path *path,
219219
int record_num, int *call_panic);
220220

221-
extern int auditd_test_task(const struct task_struct *task);
221+
extern int auditd_test_task(struct task_struct *task);
222222

223223
#define AUDIT_INODE_BUCKETS 32
224224
extern struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];

0 commit comments

Comments
 (0)