Skip to content

Commit 2a1fe21

Browse files
committed
audit: use current whenever possible
There are many places, notably audit_log_task_info() and audit_log_exit(), that take task_struct pointers but in reality they are always working on the current task. This patch eliminates the task_struct arguments and uses current directly which allows a number of cleanups as well. Acked-by: Richard Guy Briggs <rgb@redhat.com> Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent d0a3f18 commit 2a1fe21

6 files changed

Lines changed: 90 additions & 98 deletions

File tree

‎drivers/tty/tty_audit.c‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,19 @@ static void tty_audit_log(const char *description, dev_t dev,
6161
unsigned char *data, size_t size)
6262
{
6363
struct audit_buffer *ab;
64-
struct task_struct *tsk = current;
65-
pid_t pid = task_pid_nr(tsk);
66-
uid_t uid = from_kuid(&init_user_ns, task_uid(tsk));
67-
uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(tsk));
68-
unsigned int sessionid = audit_get_sessionid(tsk);
64+
pid_t pid = task_pid_nr(current);
65+
uid_t uid = from_kuid(&init_user_ns, task_uid(current));
66+
uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
67+
unsigned int sessionid = audit_get_sessionid(current);
6968

7069
ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
7170
if (ab) {
72-
char name[sizeof(tsk->comm)];
71+
char name[sizeof(current->comm)];
7372

7473
audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
7574
" minor=%d comm=", description, pid, uid,
7675
loginuid, sessionid, MAJOR(dev), MINOR(dev));
77-
get_task_comm(name, tsk);
76+
get_task_comm(name, current);
7877
audit_log_untrustedstring(ab, name);
7978
audit_log_format(ab, " data=");
8079
audit_log_n_hex(ab, data, size);

‎include/linux/audit.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ extern void audit_log_link_denied(const char *operation);
151151
extern void audit_log_lost(const char *message);
152152

153153
extern int audit_log_task_context(struct audit_buffer *ab);
154-
extern void audit_log_task_info(struct audit_buffer *ab,
155-
struct task_struct *tsk);
154+
extern void audit_log_task_info(struct audit_buffer *ab);
156155

157156
extern int audit_update_lsm_rules(void);
158157

@@ -200,8 +199,7 @@ static inline int audit_log_task_context(struct audit_buffer *ab)
200199
{
201200
return 0;
202201
}
203-
static inline void audit_log_task_info(struct audit_buffer *ab,
204-
struct task_struct *tsk)
202+
static inline void audit_log_task_info(struct audit_buffer *ab)
205203
{ }
206204
#define audit_enabled AUDIT_OFF
207205
#endif /* CONFIG_AUDIT */

‎kernel/audit.c‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,10 +1096,11 @@ static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature
10961096

10971097
if (audit_enabled == AUDIT_OFF)
10981098
return;
1099+
10991100
ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_FEATURE_CHANGE);
11001101
if (!ab)
11011102
return;
1102-
audit_log_task_info(ab, current);
1103+
audit_log_task_info(ab);
11031104
audit_log_format(ab, " feature=%s old=%u new=%u old_lock=%u new_lock=%u res=%d",
11041105
audit_feature_names[which], !!old_feature, !!new_feature,
11051106
!!old_lock, !!new_lock, res);
@@ -2246,15 +2247,15 @@ void audit_log_d_path_exe(struct audit_buffer *ab,
22462247
audit_log_format(ab, " exe=(null)");
22472248
}
22482249

2249-
struct tty_struct *audit_get_tty(struct task_struct *tsk)
2250+
struct tty_struct *audit_get_tty(void)
22502251
{
22512252
struct tty_struct *tty = NULL;
22522253
unsigned long flags;
22532254

2254-
spin_lock_irqsave(&tsk->sighand->siglock, flags);
2255-
if (tsk->signal)
2256-
tty = tty_kref_get(tsk->signal->tty);
2257-
spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
2255+
spin_lock_irqsave(&current->sighand->siglock, flags);
2256+
if (current->signal)
2257+
tty = tty_kref_get(current->signal->tty);
2258+
spin_unlock_irqrestore(&current->sighand->siglock, flags);
22582259
return tty;
22592260
}
22602261

@@ -2263,25 +2264,24 @@ void audit_put_tty(struct tty_struct *tty)
22632264
tty_kref_put(tty);
22642265
}
22652266

2266-
void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
2267+
void audit_log_task_info(struct audit_buffer *ab)
22672268
{
22682269
const struct cred *cred;
2269-
char comm[sizeof(tsk->comm)];
2270+
char comm[sizeof(current->comm)];
22702271
struct tty_struct *tty;
22712272

22722273
if (!ab)
22732274
return;
22742275

2275-
/* tsk == current */
22762276
cred = current_cred();
2277-
tty = audit_get_tty(tsk);
2277+
tty = audit_get_tty();
22782278
audit_log_format(ab,
22792279
" ppid=%d pid=%d auid=%u uid=%u gid=%u"
22802280
" euid=%u suid=%u fsuid=%u"
22812281
" egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
2282-
task_ppid_nr(tsk),
2283-
task_tgid_nr(tsk),
2284-
from_kuid(&init_user_ns, audit_get_loginuid(tsk)),
2282+
task_ppid_nr(current),
2283+
task_tgid_nr(current),
2284+
from_kuid(&init_user_ns, audit_get_loginuid(current)),
22852285
from_kuid(&init_user_ns, cred->uid),
22862286
from_kgid(&init_user_ns, cred->gid),
22872287
from_kuid(&init_user_ns, cred->euid),
@@ -2291,11 +2291,11 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
22912291
from_kgid(&init_user_ns, cred->sgid),
22922292
from_kgid(&init_user_ns, cred->fsgid),
22932293
tty ? tty_name(tty) : "(none)",
2294-
audit_get_sessionid(tsk));
2294+
audit_get_sessionid(current));
22952295
audit_put_tty(tty);
22962296
audit_log_format(ab, " comm=");
2297-
audit_log_untrustedstring(ab, get_task_comm(comm, tsk));
2298-
audit_log_d_path_exe(ab, tsk->mm);
2297+
audit_log_untrustedstring(ab, get_task_comm(comm, current));
2298+
audit_log_d_path_exe(ab, current->mm);
22992299
audit_log_task_context(ab);
23002300
}
23012301
EXPORT_SYMBOL(audit_log_task_info);
@@ -2316,7 +2316,7 @@ void audit_log_link_denied(const char *operation)
23162316
if (!ab)
23172317
return;
23182318
audit_log_format(ab, "op=%s", operation);
2319-
audit_log_task_info(ab, current);
2319+
audit_log_task_info(ab);
23202320
audit_log_format(ab, " res=0");
23212321
audit_log_end(ab);
23222322
}

‎kernel/audit.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ extern struct audit_entry *audit_dupe_rule(struct audit_krule *old);
264264
extern void audit_log_d_path_exe(struct audit_buffer *ab,
265265
struct mm_struct *mm);
266266

267-
extern struct tty_struct *audit_get_tty(struct task_struct *tsk);
267+
extern struct tty_struct *audit_get_tty(void);
268268
extern void audit_put_tty(struct tty_struct *tty);
269269

270270
/* audit watch functions */

‎kernel/auditsc.c‎

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -830,44 +830,6 @@ void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
830830
rcu_read_unlock();
831831
}
832832

833-
/* Transfer the audit context pointer to the caller, clearing it in the tsk's struct */
834-
static inline struct audit_context *audit_take_context(struct task_struct *tsk,
835-
int return_valid,
836-
long return_code)
837-
{
838-
struct audit_context *context = tsk->audit_context;
839-
840-
if (!context)
841-
return NULL;
842-
context->return_valid = return_valid;
843-
844-
/*
845-
* we need to fix up the return code in the audit logs if the actual
846-
* return codes are later going to be fixed up by the arch specific
847-
* signal handlers
848-
*
849-
* This is actually a test for:
850-
* (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
851-
* (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
852-
*
853-
* but is faster than a bunch of ||
854-
*/
855-
if (unlikely(return_code <= -ERESTARTSYS) &&
856-
(return_code >= -ERESTART_RESTARTBLOCK) &&
857-
(return_code != -ENOIOCTLCMD))
858-
context->return_code = -EINTR;
859-
else
860-
context->return_code = return_code;
861-
862-
if (context->in_syscall && !context->dummy) {
863-
audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
864-
audit_filter_inodes(tsk, context);
865-
}
866-
867-
audit_set_context(tsk, NULL);
868-
return context;
869-
}
870-
871833
static inline void audit_proctitle_free(struct audit_context *context)
872834
{
873835
kfree(context->proctitle.value);
@@ -1296,15 +1258,18 @@ static inline int audit_proctitle_rtrim(char *proctitle, int len)
12961258
return len;
12971259
}
12981260

1299-
static void audit_log_proctitle(struct task_struct *tsk,
1300-
struct audit_context *context)
1261+
static void audit_log_proctitle(void)
13011262
{
13021263
int res;
13031264
char *buf;
13041265
char *msg = "(null)";
13051266
int len = strlen(msg);
1267+
struct audit_context *context = audit_context();
13061268
struct audit_buffer *ab;
13071269

1270+
if (!context || context->dummy)
1271+
return;
1272+
13081273
ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
13091274
if (!ab)
13101275
return; /* audit_panic or being filtered */
@@ -1317,7 +1282,7 @@ static void audit_log_proctitle(struct task_struct *tsk,
13171282
if (!buf)
13181283
goto out;
13191284
/* Historically called this from procfs naming */
1320-
res = get_cmdline(tsk, buf, MAX_PROCTITLE_AUDIT_LEN);
1285+
res = get_cmdline(current, buf, MAX_PROCTITLE_AUDIT_LEN);
13211286
if (res == 0) {
13221287
kfree(buf);
13231288
goto out;
@@ -1337,15 +1302,15 @@ static void audit_log_proctitle(struct task_struct *tsk,
13371302
audit_log_end(ab);
13381303
}
13391304

1340-
static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
1305+
static void audit_log_exit(void)
13411306
{
13421307
int i, call_panic = 0;
1308+
struct audit_context *context = audit_context();
13431309
struct audit_buffer *ab;
13441310
struct audit_aux_data *aux;
13451311
struct audit_names *n;
13461312

1347-
/* tsk == current */
1348-
context->personality = tsk->personality;
1313+
context->personality = current->personality;
13491314

13501315
ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
13511316
if (!ab)
@@ -1367,7 +1332,7 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
13671332
context->argv[3],
13681333
context->name_count);
13691334

1370-
audit_log_task_info(ab, tsk);
1335+
audit_log_task_info(ab);
13711336
audit_log_key(ab, context->filterkey);
13721337
audit_log_end(ab);
13731338

@@ -1456,7 +1421,7 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
14561421
audit_log_name(context, n, NULL, i++, &call_panic);
14571422
}
14581423

1459-
audit_log_proctitle(tsk, context);
1424+
audit_log_proctitle();
14601425

14611426
/* Send end of event record to help user space know we are finished */
14621427
ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
@@ -1474,22 +1439,31 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
14741439
*/
14751440
void __audit_free(struct task_struct *tsk)
14761441
{
1477-
struct audit_context *context;
1442+
struct audit_context *context = tsk->audit_context;
14781443

1479-
context = audit_take_context(tsk, 0, 0);
14801444
if (!context)
14811445
return;
14821446

1483-
/* Check for system calls that do not go through the exit
1484-
* function (e.g., exit_group), then free context block.
1485-
* We use GFP_ATOMIC here because we might be doing this
1486-
* in the context of the idle thread */
1487-
/* that can happen only if we are called from do_exit() */
1488-
if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1489-
audit_log_exit(context, tsk);
1447+
/* We are called either by do_exit() or the fork() error handling code;
1448+
* in the former case tsk == current and in the latter tsk is a
1449+
* random task_struct that doesn't doesn't have any meaningful data we
1450+
* need to log via audit_log_exit().
1451+
*/
1452+
if (tsk == current && !context->dummy && context->in_syscall) {
1453+
context->return_valid = 0;
1454+
context->return_code = 0;
1455+
1456+
audit_filter_syscall(tsk, context,
1457+
&audit_filter_list[AUDIT_FILTER_EXIT]);
1458+
audit_filter_inodes(tsk, context);
1459+
if (context->current_state == AUDIT_RECORD_CONTEXT)
1460+
audit_log_exit();
1461+
}
1462+
14901463
if (!list_empty(&context->killed_trees))
14911464
audit_kill_trees(&context->killed_trees);
14921465

1466+
audit_set_context(tsk, NULL);
14931467
audit_free_context(context);
14941468
}
14951469

@@ -1559,17 +1533,40 @@ void __audit_syscall_exit(int success, long return_code)
15591533
{
15601534
struct audit_context *context;
15611535

1562-
if (success)
1563-
success = AUDITSC_SUCCESS;
1564-
else
1565-
success = AUDITSC_FAILURE;
1566-
1567-
context = audit_take_context(current, success, return_code);
1536+
context = audit_context();
15681537
if (!context)
15691538
return;
15701539

1571-
if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
1572-
audit_log_exit(context, current);
1540+
if (!context->dummy && context->in_syscall) {
1541+
if (success)
1542+
context->return_valid = AUDITSC_SUCCESS;
1543+
else
1544+
context->return_valid = AUDITSC_FAILURE;
1545+
1546+
/*
1547+
* we need to fix up the return code in the audit logs if the
1548+
* actual return codes are later going to be fixed up by the
1549+
* arch specific signal handlers
1550+
*
1551+
* This is actually a test for:
1552+
* (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
1553+
* (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
1554+
*
1555+
* but is faster than a bunch of ||
1556+
*/
1557+
if (unlikely(return_code <= -ERESTARTSYS) &&
1558+
(return_code >= -ERESTART_RESTARTBLOCK) &&
1559+
(return_code != -ENOIOCTLCMD))
1560+
context->return_code = -EINTR;
1561+
else
1562+
context->return_code = return_code;
1563+
1564+
audit_filter_syscall(current, context,
1565+
&audit_filter_list[AUDIT_FILTER_EXIT]);
1566+
audit_filter_inodes(current, context);
1567+
if (context->current_state == AUDIT_RECORD_CONTEXT)
1568+
audit_log_exit();
1569+
}
15731570

15741571
context->in_syscall = 0;
15751572
context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
@@ -1591,7 +1588,6 @@ void __audit_syscall_exit(int success, long return_code)
15911588
kfree(context->filterkey);
15921589
context->filterkey = NULL;
15931590
}
1594-
audit_set_context(current, context);
15951591
}
15961592

15971593
static inline void handle_one(const struct inode *inode)
@@ -2025,7 +2021,7 @@ static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
20252021
uid = from_kuid(&init_user_ns, task_uid(current));
20262022
oldloginuid = from_kuid(&init_user_ns, koldloginuid);
20272023
loginuid = from_kuid(&init_user_ns, kloginuid),
2028-
tty = audit_get_tty(current);
2024+
tty = audit_get_tty();
20292025

20302026
audit_log_format(ab, "pid=%d uid=%u", task_tgid_nr(current), uid);
20312027
audit_log_task_context(ab);
@@ -2046,7 +2042,6 @@ static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
20462042
*/
20472043
int audit_set_loginuid(kuid_t loginuid)
20482044
{
2049-
struct task_struct *task = current;
20502045
unsigned int oldsessionid, sessionid = AUDIT_SID_UNSET;
20512046
kuid_t oldloginuid;
20522047
int rc;
@@ -2065,8 +2060,8 @@ int audit_set_loginuid(kuid_t loginuid)
20652060
sessionid = (unsigned int)atomic_inc_return(&session_id);
20662061
}
20672062

2068-
task->sessionid = sessionid;
2069-
task->loginuid = loginuid;
2063+
current->sessionid = sessionid;
2064+
current->loginuid = loginuid;
20702065
out:
20712066
audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
20722067
return rc;

‎security/integrity/ima/ima_api.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void ima_audit_measurement(struct integrity_iint_cache *iint,
336336
audit_log_untrustedstring(ab, filename);
337337
audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash);
338338

339-
audit_log_task_info(ab, current);
339+
audit_log_task_info(ab);
340340
audit_log_end(ab);
341341

342342
iint->flags |= IMA_AUDITED;

0 commit comments

Comments
 (0)