]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - drivers/tty/tty_audit.c
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[mirror_ubuntu-bionic-kernel.git] / drivers / tty / tty_audit.c
index 79439846d29da5b2a3995ea2d6514e9e27b893b7..df2d735338e216d0a398c50a7e4202afd21417e6 100644 (file)
@@ -14,7 +14,6 @@
 #include <linux/tty.h>
 
 struct tty_audit_buf {
-       atomic_t count;
        struct mutex mutex;     /* Protects all data below */
        dev_t dev;              /* The TTY which the data is from */
        unsigned icanon:1;
@@ -22,6 +21,15 @@ struct tty_audit_buf {
        unsigned char *data;    /* Allocated size N_TTY_BUF_SIZE */
 };
 
+static struct tty_audit_buf *tty_audit_buf_ref(void)
+{
+       struct tty_audit_buf *buf;
+
+       buf = current->signal->tty_audit_buf;
+       WARN_ON(buf == ERR_PTR(-ESRCH));
+       return buf;
+}
+
 static struct tty_audit_buf *tty_audit_buf_alloc(void)
 {
        struct tty_audit_buf *buf;
@@ -32,7 +40,6 @@ static struct tty_audit_buf *tty_audit_buf_alloc(void)
        buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
        if (!buf->data)
                goto err_buf;
-       atomic_set(&buf->count, 1);
        mutex_init(&buf->mutex);
        buf->dev = MKDEV(0, 0);
        buf->icanon = 0;
@@ -52,12 +59,6 @@ static void tty_audit_buf_free(struct tty_audit_buf *buf)
        kfree(buf);
 }
 
-static void tty_audit_buf_put(struct tty_audit_buf *buf)
-{
-       if (atomic_dec_and_test(&buf->count))
-               tty_audit_buf_free(buf);
-}
-
 static void tty_audit_log(const char *description, dev_t dev,
                          unsigned char *data, size_t size)
 {
@@ -106,21 +107,20 @@ static void tty_audit_buf_push(struct tty_audit_buf *buf)
  *
  *     Make sure all buffered data is written out and deallocate the buffer.
  *     Only needs to be called if current->signal->tty_audit_buf != %NULL.
+ *
+ *     The process is single-threaded at this point; no other threads share
+ *     current->signal.
  */
 void tty_audit_exit(void)
 {
        struct tty_audit_buf *buf;
 
-       buf = current->signal->tty_audit_buf;
-       current->signal->tty_audit_buf = NULL;
+       buf = xchg(&current->signal->tty_audit_buf, ERR_PTR(-ESRCH));
        if (!buf)
                return;
 
-       mutex_lock(&buf->mutex);
        tty_audit_buf_push(buf);
-       mutex_unlock(&buf->mutex);
-
-       tty_audit_buf_put(buf);
+       tty_audit_buf_free(buf);
 }
 
 /**
@@ -138,33 +138,14 @@ void tty_audit_fork(struct signal_struct *sig)
  */
 void tty_audit_tiocsti(struct tty_struct *tty, char ch)
 {
-       struct tty_audit_buf *buf;
        dev_t dev;
-       unsigned long flags;
-
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       buf = current->signal->tty_audit_buf;
-       if (buf)
-               atomic_inc(&buf->count);
-       spin_unlock_irqrestore(&current->sighand->siglock, flags);
 
        dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
-       if (buf) {
-               mutex_lock(&buf->mutex);
-               if (buf->dev == dev)
-                       tty_audit_buf_push(buf);
-               mutex_unlock(&buf->mutex);
-               tty_audit_buf_put(buf);
-       }
-
-       if (audit_enabled && (current->signal->audit_tty & AUDIT_TTY_ENABLE)) {
-               kuid_t auid;
-               unsigned int sessionid;
+       if (tty_audit_push())
+               return;
 
-               auid = audit_get_loginuid(current);
-               sessionid = audit_get_sessionid(current);
+       if (audit_enabled)
                tty_audit_log("ioctl=TIOCSTI", dev, &ch, 1);
-       }
 }
 
 /**
@@ -175,23 +156,15 @@ void tty_audit_tiocsti(struct tty_struct *tty, char ch)
 int tty_audit_push(void)
 {
        struct tty_audit_buf *buf;
-       unsigned long flags;
 
        if (~current->signal->audit_tty & AUDIT_TTY_ENABLE)
                return -EPERM;
 
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       buf = current->signal->tty_audit_buf;
-       if (buf)
-               atomic_inc(&buf->count);
-       spin_unlock_irqrestore(&current->sighand->siglock, flags);
-
-       if (buf) {
+       buf = tty_audit_buf_ref();
+       if (!IS_ERR_OR_NULL(buf)) {
                mutex_lock(&buf->mutex);
                tty_audit_buf_push(buf);
                mutex_unlock(&buf->mutex);
-
-               tty_audit_buf_put(buf);
        }
        return 0;
 }
@@ -200,43 +173,27 @@ int tty_audit_push(void)
  *     tty_audit_buf_get       -       Get an audit buffer.
  *
  *     Get an audit buffer, allocate it if necessary.  Return %NULL
- *     if out of memory.  Otherwise, return a new reference to the buffer.
+ *     if out of memory or ERR_PTR(-ESRCH) if tty_audit_exit() has already
+ *     occurred.  Otherwise, return a new reference to the buffer.
  */
 static struct tty_audit_buf *tty_audit_buf_get(void)
 {
-       struct tty_audit_buf *buf, *buf2;
-       unsigned long flags;
+       struct tty_audit_buf *buf;
 
-       buf = NULL;
-       buf2 = NULL;
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       buf = current->signal->tty_audit_buf;
-       if (buf) {
-               atomic_inc(&buf->count);
-               goto out;
-       }
-       spin_unlock_irqrestore(&current->sighand->siglock, flags);
+       buf = tty_audit_buf_ref();
+       if (buf)
+               return buf;
 
-       buf2 = tty_audit_buf_alloc();
-       if (buf2 == NULL) {
+       buf = tty_audit_buf_alloc();
+       if (buf == NULL) {
                audit_log_lost("out of memory in TTY auditing");
                return NULL;
        }
 
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       buf = current->signal->tty_audit_buf;
-       if (!buf) {
-               current->signal->tty_audit_buf = buf2;
-               buf = buf2;
-               buf2 = NULL;
-       }
-       atomic_inc(&buf->count);
-       /* Fall through */
- out:
-       spin_unlock_irqrestore(&current->sighand->siglock, flags);
-       if (buf2)
-               tty_audit_buf_free(buf2);
-       return buf;
+       /* Race to use this buffer, free it if another wins */
+       if (cmpxchg(&current->signal->tty_audit_buf, NULL, buf) != NULL)
+               tty_audit_buf_free(buf);
+       return tty_audit_buf_ref();
 }
 
 /**
@@ -251,6 +208,10 @@ void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
        unsigned int audit_tty;
        dev_t dev;
 
+       audit_tty = READ_ONCE(current->signal->audit_tty);
+       if (~audit_tty & AUDIT_TTY_ENABLE)
+               return;
+
        if (unlikely(size == 0))
                return;
 
@@ -258,14 +219,11 @@ void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
            && tty->driver->subtype == PTY_TYPE_MASTER)
                return;
 
-       audit_tty = READ_ONCE(current->signal->audit_tty);
-       if (~audit_tty & AUDIT_TTY_ENABLE)
-               return;
        if ((~audit_tty & AUDIT_TTY_LOG_PASSWD) && icanon && !L_ECHO(tty))
                return;
 
        buf = tty_audit_buf_get();
-       if (!buf)
+       if (IS_ERR_OR_NULL(buf))
                return;
 
        mutex_lock(&buf->mutex);
@@ -289,5 +247,4 @@ void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
                        tty_audit_buf_push(buf);
        } while (size != 0);
        mutex_unlock(&buf->mutex);
-       tty_audit_buf_put(buf);
 }