Skip to content

Commit

Permalink
audit: add tty field to LOGIN event
Browse files Browse the repository at this point in the history
The tty field was missing from AUDIT_LOGIN events.

Refactor code to create a new function audit_get_tty(), using it to
replace the call in audit_log_task_info() and to add it to
audit_log_set_loginuid().  Lock and bump the kref to protect it, adding
audit_put_tty() alias to decrement it.

Signed-off-by: Richard Guy Briggs <[email protected]>
Signed-off-by: Paul Moore <[email protected]>
  • Loading branch information
rgbriggs authored and pcmoore committed Apr 26, 2016
1 parent 7ffb8e3 commit db0a6fb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
24 changes: 24 additions & 0 deletions include/linux/audit.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <linux/sched.h>
#include <linux/ptrace.h>
#include <uapi/linux/audit.h>
#include <linux/tty.h>

#define AUDIT_INO_UNSET ((unsigned long)-1)
#define AUDIT_DEV_UNSET ((dev_t)-1)
Expand Down Expand Up @@ -343,6 +344,23 @@ static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
return tsk->sessionid;
}

static inline struct tty_struct *audit_get_tty(struct task_struct *tsk)
{
struct tty_struct *tty = NULL;
unsigned long flags;

spin_lock_irqsave(&tsk->sighand->siglock, flags);
if (tsk->signal)
tty = tty_kref_get(tsk->signal->tty);
spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
return tty;
}

static inline void audit_put_tty(struct tty_struct *tty)
{
tty_kref_put(tty);
}

extern void __audit_ipc_obj(struct kern_ipc_perm *ipcp);
extern void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode);
extern void __audit_bprm(struct linux_binprm *bprm);
Expand Down Expand Up @@ -500,6 +518,12 @@ static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
{
return -1;
}
static inline struct tty_struct *audit_get_tty(struct task_struct *tsk)
{
return NULL;
}
static inline void audit_put_tty(struct tty_struct *tty)
{ }
static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
{ }
static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
Expand Down
18 changes: 5 additions & 13 deletions kernel/audit.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
#include <linux/security.h>
#endif
#include <linux/freezer.h>
#include <linux/tty.h>
#include <linux/pid_namespace.h>
#include <net/netns/generic.h>

Expand Down Expand Up @@ -1871,21 +1870,14 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
{
const struct cred *cred;
char comm[sizeof(tsk->comm)];
char *tty;
struct tty_struct *tty;

if (!ab)
return;

/* tsk == current */
cred = current_cred();

spin_lock_irq(&tsk->sighand->siglock);
if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
tty = tsk->signal->tty->name;
else
tty = "(none)";
spin_unlock_irq(&tsk->sighand->siglock);

tty = audit_get_tty(tsk);
audit_log_format(ab,
" ppid=%d pid=%d auid=%u uid=%u gid=%u"
" euid=%u suid=%u fsuid=%u"
Expand All @@ -1901,11 +1893,11 @@ void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
from_kgid(&init_user_ns, cred->egid),
from_kgid(&init_user_ns, cred->sgid),
from_kgid(&init_user_ns, cred->fsgid),
tty, audit_get_sessionid(tsk));

tty ? tty_name(tty) : "(none)",
audit_get_sessionid(tsk));
audit_put_tty(tty);
audit_log_format(ab, " comm=");
audit_log_untrustedstring(ab, get_task_comm(comm, tsk));

audit_log_d_path_exe(ab, tsk->mm);
audit_log_task_context(ab);
}
Expand Down
8 changes: 6 additions & 2 deletions kernel/auditsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1980,21 +1980,25 @@ static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
{
struct audit_buffer *ab;
uid_t uid, oldloginuid, loginuid;
struct tty_struct *tty;

if (!audit_enabled)
return;

uid = from_kuid(&init_user_ns, task_uid(current));
oldloginuid = from_kuid(&init_user_ns, koldloginuid);
loginuid = from_kuid(&init_user_ns, kloginuid),
tty = audit_get_tty(current);

ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
if (!ab)
return;
audit_log_format(ab, "pid=%d uid=%u", task_pid_nr(current), uid);
audit_log_task_context(ab);
audit_log_format(ab, " old-auid=%u auid=%u old-ses=%u ses=%u res=%d",
oldloginuid, loginuid, oldsessionid, sessionid, !rc);
audit_log_format(ab, " old-auid=%u auid=%u tty=%s old-ses=%u ses=%u res=%d",
oldloginuid, loginuid, tty ? tty_name(tty) : "(none)",
oldsessionid, sessionid, !rc);
audit_put_tty(tty);
audit_log_end(ab);
}

Expand Down

0 comments on commit db0a6fb

Please sign in to comment.