Merge tag 'notifications-20200601' of git://git.kernel.org/pub/scm/linux/kernel/git...
authorLinus Torvalds <torvalds@linux-foundation.org>
Sat, 13 Jun 2020 16:56:21 +0000 (09:56 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 13 Jun 2020 16:56:21 +0000 (09:56 -0700)
Pull notification queue from David Howells:
 "This adds a general notification queue concept and adds an event
  source for keys/keyrings, such as linking and unlinking keys and
  changing their attributes.

  Thanks to Debarshi Ray, we do have a pull request to use this to fix a
  problem with gnome-online-accounts - as mentioned last time:

     https://gitlab.gnome.org/GNOME/gnome-online-accounts/merge_requests/47

  Without this, g-o-a has to constantly poll a keyring-based kerberos
  cache to find out if kinit has changed anything.

  [ There are other notification pending: mount/sb fsinfo notifications
    for libmount that Karel Zak and Ian Kent have been working on, and
    Christian Brauner would like to use them in lxc, but let's see how
    this one works first ]

  LSM hooks are included:

   - A set of hooks are provided that allow an LSM to rule on whether or
     not a watch may be set. Each of these hooks takes a different
     "watched object" parameter, so they're not really shareable. The
     LSM should use current's credentials. [Wanted by SELinux & Smack]

   - A hook is provided to allow an LSM to rule on whether or not a
     particular message may be posted to a particular queue. This is
     given the credentials from the event generator (which may be the
     system) and the watch setter. [Wanted by Smack]

  I've provided SELinux and Smack with implementations of some of these
  hooks.

  WHY
  ===

  Key/keyring notifications are desirable because if you have your
  kerberos tickets in a file/directory, your Gnome desktop will monitor
  that using something like fanotify and tell you if your credentials
  cache changes.

  However, we also have the ability to cache your kerberos tickets in
  the session, user or persistent keyring so that it isn't left around
  on disk across a reboot or logout. Keyrings, however, cannot currently
  be monitored asynchronously, so the desktop has to poll for it - not
  so good on a laptop. This facility will allow the desktop to avoid the
  need to poll.

  DESIGN DECISIONS
  ================

   - The notification queue is built on top of a standard pipe. Messages
     are effectively spliced in. The pipe is opened with a special flag:

        pipe2(fds, O_NOTIFICATION_PIPE);

     The special flag has the same value as O_EXCL (which doesn't seem
     like it will ever be applicable in this context)[?]. It is given up
     front to make it a lot easier to prohibit splice&co from accessing
     the pipe.

     [?] Should this be done some other way?  I'd rather not use up a new
         O_* flag if I can avoid it - should I add a pipe3() system call
         instead?

     The pipe is then configured::

        ioctl(fds[1], IOC_WATCH_QUEUE_SET_SIZE, queue_depth);
        ioctl(fds[1], IOC_WATCH_QUEUE_SET_FILTER, &filter);

     Messages are then read out of the pipe using read().

   - It should be possible to allow write() to insert data into the
     notification pipes too, but this is currently disabled as the
     kernel has to be able to insert messages into the pipe *without*
     holding pipe->mutex and the code to make this work needs careful
     auditing.

   - sendfile(), splice() and vmsplice() are disabled on notification
     pipes because of the pipe->mutex issue and also because they
     sometimes want to revert what they just did - but one or more
     notification messages might've been interleaved in the ring.

   - The kernel inserts messages with the wait queue spinlock held. This
     means that pipe_read() and pipe_write() have to take the spinlock
     to update the queue pointers.

   - Records in the buffer are binary, typed and have a length so that
     they can be of varying size.

     This allows multiple heterogeneous sources to share a common
     buffer; there are 16 million types available, of which I've used
     just a few, so there is scope for others to be used. Tags may be
     specified when a watchpoint is created to help distinguish the
     sources.

   - Records are filterable as types have up to 256 subtypes that can be
     individually filtered. Other filtration is also available.

   - Notification pipes don't interfere with each other; each may be
     bound to a different set of watches. Any particular notification
     will be copied to all the queues that are currently watching for it
     - and only those that are watching for it.

   - When recording a notification, the kernel will not sleep, but will
     rather mark a queue as having lost a message if there's
     insufficient space. read() will fabricate a loss notification
     message at an appropriate point later.

   - The notification pipe is created and then watchpoints are attached
     to it, using one of:

        keyctl_watch_key(KEY_SPEC_SESSION_KEYRING, fds[1], 0x01);
        watch_mount(AT_FDCWD, "/", 0, fd, 0x02);
        watch_sb(AT_FDCWD, "/mnt", 0, fd, 0x03);

     where in both cases, fd indicates the queue and the number after is
     a tag between 0 and 255.

   - Watches are removed if either the notification pipe is destroyed or
     the watched object is destroyed. In the latter case, a message will
     be generated indicating the enforced watch removal.

  Things I want to avoid:

   - Introducing features that make the core VFS dependent on the
     network stack or networking namespaces (ie. usage of netlink).

   - Dumping all this stuff into dmesg and having a daemon that sits
     there parsing the output and distributing it as this then puts the
     responsibility for security into userspace and makes handling
     namespaces tricky. Further, dmesg might not exist or might be
     inaccessible inside a container.

   - Letting users see events they shouldn't be able to see.

  TESTING AND MANPAGES
  ====================

   - The keyutils tree has a pipe-watch branch that has keyctl commands
     for making use of notifications. Proposed manual pages can also be
     found on this branch, though a couple of them really need to go to
     the main manpages repository instead.

     If the kernel supports the watching of keys, then running "make
     test" on that branch will cause the testing infrastructure to spawn
     a monitoring process on the side that monitors a notifications pipe
     for all the key/keyring changes induced by the tests and they'll
     all be checked off to make sure they happened.

        https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/keyutils.git/log/?h=pipe-watch

   - A test program is provided (samples/watch_queue/watch_test) that
     can be used to monitor for keyrings, mount and superblock events.
     Information on the notifications is simply logged to stdout"

* tag 'notifications-20200601' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs:
  smack: Implement the watch_key and post_notification hooks
  selinux: Implement the watch_key security hook
  keys: Make the KEY_NEED_* perms an enum rather than a mask
  pipe: Add notification lossage handling
  pipe: Allow buffers to be marked read-whole-or-error for notifications
  Add sample notification program
  watch_queue: Add a key/keyring notification facility
  security: Add hooks to rule on setting a watch
  pipe: Add general notification queue support
  pipe: Add O_NOTIFICATION_PIPE
  security: Add a hook for the point of notification insertion
  uapi: General notification queue definitions

19 files changed:
1  2 
Documentation/security/keys/core.rst
Documentation/userspace-api/ioctl/ioctl-number.rst
fs/pipe.c
fs/splice.c
include/linux/lsm_hook_defs.h
include/linux/lsm_hooks.h
include/linux/pipe_fs_i.h
include/linux/security.h
init/Kconfig
kernel/Makefile
kernel/watch_queue.c
samples/Kconfig
samples/Makefile
security/keys/Kconfig
security/keys/internal.h
security/keys/keyctl.c
security/security.c
security/selinux/hooks.c
security/smack/smack_lsm.c

@@@ -920,14 -920,10 +920,14 @@@ The keyctl syscall functions are
  
        long keyctl(KEYCTL_PKEY_QUERY,
                    key_serial_t key_id, unsigned long reserved,
 +                  const char *params,
                    struct keyctl_pkey_query *info);
  
 -     Get information about an asymmetric key.  The information is returned in
 -     the keyctl_pkey_query struct::
 +     Get information about an asymmetric key.  Specific algorithms and
 +     encodings may be queried by using the ``params`` argument.  This is a
 +     string containing a space- or tab-separated string of key-value pairs.
 +     Currently supported keys include ``enc`` and ``hash``.  The information
 +     is returned in the keyctl_pkey_query struct::
  
        __u32   supported_ops;
        __u32   key_size;
       written into the output buffer.  Verification returns 0 on success.
  
  
+   *  Watch a key or keyring for changes::
+       long keyctl(KEYCTL_WATCH_KEY, key_serial_t key, int queue_fd,
+                   const struct watch_notification_filter *filter);
+      This will set or remove a watch for changes on the specified key or
+      keyring.
+      "key" is the ID of the key to be watched.
+      "queue_fd" is a file descriptor referring to an open "/dev/watch_queue"
+      which manages the buffer into which notifications will be delivered.
+      "filter" is either NULL to remove a watch or a filter specification to
+      indicate what events are required from the key.
+      See Documentation/watch_queue.rst for more information.
+      Note that only one watch may be emplaced for any particular { key,
+      queue_fd } combination.
+      Notification records look like::
+       struct key_notification {
+               struct watch_notification watch;
+               __u32   key_id;
+               __u32   aux;
+       };
+      In this, watch::type will be "WATCH_TYPE_KEY_NOTIFY" and subtype will be
+      one of::
+       NOTIFY_KEY_INSTANTIATED
+       NOTIFY_KEY_UPDATED
+       NOTIFY_KEY_LINKED
+       NOTIFY_KEY_UNLINKED
+       NOTIFY_KEY_CLEARED
+       NOTIFY_KEY_REVOKED
+       NOTIFY_KEY_INVALIDATED
+       NOTIFY_KEY_SETATTR
+      Where these indicate a key being instantiated/rejected, updated, a link
+      being made in a keyring, a link being removed from a keyring, a keyring
+      being cleared, a key being revoked, a key being invalidated or a key
+      having one of its attributes changed (user, group, perm, timeout,
+      restriction).
+      If a watched key is deleted, a basic watch_notification will be issued
+      with "type" set to WATCH_TYPE_META and "subtype" set to
+      watch_meta_removal_notification.  The watchpoint ID will be set in the
+      "info" field.
+      This needs to be configured by enabling:
+       "Provide key/keyring change notifications" (KEY_NOTIFICATIONS)
  Kernel Services
  ===============
  
@@@ -146,7 -146,6 +146,7 @@@ Code  Seq#    Include Fil
  'H'   40-4F  sound/hdspm.h                                           conflict!
  'H'   40-4F  sound/hdsp.h                                            conflict!
  'H'   90     sound/usb/usx2y/usb_stream.h
 +'H'   00-0F  uapi/misc/habanalabs.h                                  conflict!
  'H'   A0     uapi/linux/usb/cdc-wdm.h
  'H'   C0-F0  net/bluetooth/hci.h                                     conflict!
  'H'   C0-DF  net/bluetooth/hidp/hidp.h                               conflict!
  'W'   00-1F  linux/wanrouter.h                                       conflict! (pre 3.9)
  'W'   00-3F  sound/asound.h                                          conflict!
  'W'   40-5F  drivers/pci/switch/switchtec.c
+ 'W'   60-61  linux/watch_queue.h
  'X'   all    fs/xfs/xfs_fs.h,                                        conflict!
               fs/xfs/linux-2.6/xfs_ioctl32.h,
               include/linux/falloc.h,
  'v'   00-1F  linux/fs.h                                              conflict!
  'v'   00-0F  linux/sonypi.h                                          conflict!
  'v'   00-0F  media/v4l2-subdev.h                                     conflict!
 +'v'   20-27  arch/powerpc/include/uapi/asm/vas-api.h               VAS API
  'v'   C0-FF  linux/meye.h                                            conflict!
  'w'   all                                                            CERN SCI driver
  'y'   00-1F                                                          packet based user level communications
diff --combined fs/pipe.c
+++ b/fs/pipe.c
@@@ -24,6 -24,7 +24,7 @@@
  #include <linux/syscalls.h>
  #include <linux/fcntl.h>
  #include <linux/memcontrol.h>
+ #include <linux/watch_queue.h>
  
  #include <linux/uaccess.h>
  #include <asm/ioctls.h>
@@@ -140,20 -141,21 +141,20 @@@ static void anon_pipe_buf_release(struc
                put_page(page);
  }
  
 -static int anon_pipe_buf_steal(struct pipe_inode_info *pipe,
 -                             struct pipe_buffer *buf)
 +static bool anon_pipe_buf_try_steal(struct pipe_inode_info *pipe,
 +              struct pipe_buffer *buf)
  {
        struct page *page = buf->page;
  
 -      if (page_count(page) == 1) {
 -              memcg_kmem_uncharge_page(page, 0);
 -              __SetPageLocked(page);
 -              return 0;
 -      }
 -      return 1;
 +      if (page_count(page) != 1)
 +              return false;
 +      memcg_kmem_uncharge_page(page, 0);
 +      __SetPageLocked(page);
 +      return true;
  }
  
  /**
 - * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
 + * generic_pipe_buf_try_steal - attempt to take ownership of a &pipe_buffer
   * @pipe:     the pipe that the buffer belongs to
   * @buf:      the buffer to attempt to steal
   *
   *    he wishes; the typical use is insertion into a different file
   *    page cache.
   */
 -int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
 -                         struct pipe_buffer *buf)
 +bool generic_pipe_buf_try_steal(struct pipe_inode_info *pipe,
 +              struct pipe_buffer *buf)
  {
        struct page *page = buf->page;
  
         */
        if (page_count(page) == 1) {
                lock_page(page);
 -              return 0;
 +              return true;
        }
 -
 -      return 1;
 +      return false;
  }
 -EXPORT_SYMBOL(generic_pipe_buf_steal);
 +EXPORT_SYMBOL(generic_pipe_buf_try_steal);
  
  /**
   * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
@@@ -199,6 -202,22 +200,6 @@@ bool generic_pipe_buf_get(struct pipe_i
  EXPORT_SYMBOL(generic_pipe_buf_get);
  
  /**
 - * generic_pipe_buf_confirm - verify contents of the pipe buffer
 - * @info:     the pipe that the buffer belongs to
 - * @buf:      the buffer to confirm
 - *
 - * Description:
 - *    This function does nothing, because the generic pipe code uses
 - *    pages that are always good when inserted into the pipe.
 - */
 -int generic_pipe_buf_confirm(struct pipe_inode_info *info,
 -                           struct pipe_buffer *buf)
 -{
 -      return 0;
 -}
 -EXPORT_SYMBOL(generic_pipe_buf_confirm);
 -
 -/**
   * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
   * @pipe:     the pipe that the buffer belongs to
   * @buf:      the buffer to put a reference to
@@@ -213,12 -232,48 +214,12 @@@ void generic_pipe_buf_release(struct pi
  }
  EXPORT_SYMBOL(generic_pipe_buf_release);
  
 -/* New data written to a pipe may be appended to a buffer with this type. */
  static const struct pipe_buf_operations anon_pipe_buf_ops = {
 -      .confirm = generic_pipe_buf_confirm,
 -      .release = anon_pipe_buf_release,
 -      .steal = anon_pipe_buf_steal,
 -      .get = generic_pipe_buf_get,
 -};
 -
 -static const struct pipe_buf_operations anon_pipe_buf_nomerge_ops = {
 -      .confirm = generic_pipe_buf_confirm,
 -      .release = anon_pipe_buf_release,
 -      .steal = anon_pipe_buf_steal,
 -      .get = generic_pipe_buf_get,
 +      .release        = anon_pipe_buf_release,
 +      .try_steal      = anon_pipe_buf_try_steal,
 +      .get            = generic_pipe_buf_get,
  };
  
 -static const struct pipe_buf_operations packet_pipe_buf_ops = {
 -      .confirm = generic_pipe_buf_confirm,
 -      .release = anon_pipe_buf_release,
 -      .steal = anon_pipe_buf_steal,
 -      .get = generic_pipe_buf_get,
 -};
 -
 -/**
 - * pipe_buf_mark_unmergeable - mark a &struct pipe_buffer as unmergeable
 - * @buf:      the buffer to mark
 - *
 - * Description:
 - *    This function ensures that no future writes will be merged into the
 - *    given &struct pipe_buffer. This is necessary when multiple pipe buffers
 - *    share the same backing page.
 - */
 -void pipe_buf_mark_unmergeable(struct pipe_buffer *buf)
 -{
 -      if (buf->ops == &anon_pipe_buf_ops)
 -              buf->ops = &anon_pipe_buf_nomerge_ops;
 -}
 -
 -static bool pipe_buf_can_merge(struct pipe_buffer *buf)
 -{
 -      return buf->ops == &anon_pipe_buf_ops;
 -}
 -
  /* Done while waiting without holding the pipe lock - thus the READ_ONCE() */
  static inline bool pipe_readable(const struct pipe_inode_info *pipe)
  {
@@@ -259,14 -314,44 +260,44 @@@ pipe_read(struct kiocb *iocb, struct io
                unsigned int tail = pipe->tail;
                unsigned int mask = pipe->ring_size - 1;
  
+ #ifdef CONFIG_WATCH_QUEUE
+               if (pipe->note_loss) {
+                       struct watch_notification n;
+                       if (total_len < 8) {
+                               if (ret == 0)
+                                       ret = -ENOBUFS;
+                               break;
+                       }
+                       n.type = WATCH_TYPE_META;
+                       n.subtype = WATCH_META_LOSS_NOTIFICATION;
+                       n.info = watch_sizeof(n);
+                       if (copy_to_iter(&n, sizeof(n), to) != sizeof(n)) {
+                               if (ret == 0)
+                                       ret = -EFAULT;
+                               break;
+                       }
+                       ret += sizeof(n);
+                       total_len -= sizeof(n);
+                       pipe->note_loss = false;
+               }
+ #endif
                if (!pipe_empty(head, tail)) {
                        struct pipe_buffer *buf = &pipe->bufs[tail & mask];
                        size_t chars = buf->len;
                        size_t written;
                        int error;
  
-                       if (chars > total_len)
+                       if (chars > total_len) {
+                               if (buf->flags & PIPE_BUF_FLAG_WHOLE) {
+                                       if (ret == 0)
+                                               ret = -ENOBUFS;
+                                       break;
+                               }
                                chars = total_len;
+                       }
  
                        error = pipe_buf_confirm(pipe, buf);
                        if (error) {
                        if (!buf->len) {
                                pipe_buf_release(pipe, buf);
                                spin_lock_irq(&pipe->rd_wait.lock);
+ #ifdef CONFIG_WATCH_QUEUE
+                               if (buf->flags & PIPE_BUF_FLAG_LOSS)
+                                       pipe->note_loss = true;
+ #endif
                                tail++;
                                pipe->tail = tail;
                                spin_unlock_irq(&pipe->rd_wait.lock);
@@@ -405,6 -494,13 +440,13 @@@ pipe_write(struct kiocb *iocb, struct i
                goto out;
        }
  
+ #ifdef CONFIG_WATCH_QUEUE
+       if (pipe->watch_queue) {
+               ret = -EXDEV;
+               goto out;
+       }
+ #endif
        /*
         * Only wake up if the pipe started out empty, since
         * otherwise there should be no readers waiting.
                struct pipe_buffer *buf = &pipe->bufs[(head - 1) & mask];
                int offset = buf->offset + buf->len;
  
 -              if (pipe_buf_can_merge(buf) && offset + chars <= PAGE_SIZE) {
 +              if ((buf->flags & PIPE_BUF_FLAG_CAN_MERGE) &&
 +                  offset + chars <= PAGE_SIZE) {
                        ret = pipe_buf_confirm(pipe, buf);
                        if (ret)
                                goto out;
                        buf->ops = &anon_pipe_buf_ops;
                        buf->offset = 0;
                        buf->len = 0;
 -                      buf->flags = 0;
 -                      if (is_packetized(filp)) {
 -                              buf->ops = &packet_pipe_buf_ops;
 +                      if (is_packetized(filp))
                                buf->flags = PIPE_BUF_FLAG_PACKET;
 -                      }
 +                      else
 +                              buf->flags = PIPE_BUF_FLAG_CAN_MERGE;
                        pipe->tmp_page = NULL;
  
                        copied = copy_page_from_iter(page, 0, PAGE_SIZE, from);
@@@ -574,22 -670,37 +616,37 @@@ static long pipe_ioctl(struct file *fil
        int count, head, tail, mask;
  
        switch (cmd) {
-               case FIONREAD:
-                       __pipe_lock(pipe);
-                       count = 0;
-                       head = pipe->head;
-                       tail = pipe->tail;
-                       mask = pipe->ring_size - 1;
+       case FIONREAD:
+               __pipe_lock(pipe);
+               count = 0;
+               head = pipe->head;
+               tail = pipe->tail;
+               mask = pipe->ring_size - 1;
  
-                       while (tail != head) {
-                               count += pipe->bufs[tail & mask].len;
-                               tail++;
-                       }
-                       __pipe_unlock(pipe);
+               while (tail != head) {
+                       count += pipe->bufs[tail & mask].len;
+                       tail++;
+               }
+               __pipe_unlock(pipe);
+               return put_user(count, (int __user *)arg);
  
-                       return put_user(count, (int __user *)arg);
-               default:
-                       return -ENOIOCTLCMD;
+ #ifdef CONFIG_WATCH_QUEUE
+       case IOC_WATCH_QUEUE_SET_SIZE: {
+               int ret;
+               __pipe_lock(pipe);
+               ret = watch_queue_set_size(pipe, arg);
+               __pipe_unlock(pipe);
+               return ret;
+       }
+       case IOC_WATCH_QUEUE_SET_FILTER:
+               return watch_queue_set_filter(
+                       pipe, (struct watch_notification_filter __user *)arg);
+ #endif
+       default:
+               return -ENOIOCTLCMD;
        }
  }
  
@@@ -700,27 -811,27 +757,27 @@@ pipe_fasync(int fd, struct file *filp, 
        return retval;
  }
  
static unsigned long account_pipe_buffers(struct user_struct *user,
-                                  unsigned long old, unsigned long new)
+ unsigned long account_pipe_buffers(struct user_struct *user,
+                                  unsigned long old, unsigned long new)
  {
        return atomic_long_add_return(new - old, &user->pipe_bufs);
  }
  
static bool too_many_pipe_buffers_soft(unsigned long user_bufs)
+ bool too_many_pipe_buffers_soft(unsigned long user_bufs)
  {
        unsigned long soft_limit = READ_ONCE(pipe_user_pages_soft);
  
        return soft_limit && user_bufs > soft_limit;
  }
  
static bool too_many_pipe_buffers_hard(unsigned long user_bufs)
+ bool too_many_pipe_buffers_hard(unsigned long user_bufs)
  {
        unsigned long hard_limit = READ_ONCE(pipe_user_pages_hard);
  
        return hard_limit && user_bufs > hard_limit;
  }
  
static bool is_unprivileged_user(void)
bool pipe_is_unprivileged_user(void)
  {
        return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
  }
@@@ -742,12 -853,12 +799,12 @@@ struct pipe_inode_info *alloc_pipe_info
  
        user_bufs = account_pipe_buffers(user, 0, pipe_bufs);
  
-       if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) {
+       if (too_many_pipe_buffers_soft(user_bufs) && pipe_is_unprivileged_user()) {
                user_bufs = account_pipe_buffers(user, pipe_bufs, 1);
                pipe_bufs = 1;
        }
  
-       if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())
+       if (too_many_pipe_buffers_hard(user_bufs) && pipe_is_unprivileged_user())
                goto out_revert_acct;
  
        pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer),
                pipe->r_counter = pipe->w_counter = 1;
                pipe->max_usage = pipe_bufs;
                pipe->ring_size = pipe_bufs;
+               pipe->nr_accounted = pipe_bufs;
                pipe->user = user;
                mutex_init(&pipe->mutex);
                return pipe;
@@@ -776,7 -888,14 +834,14 @@@ void free_pipe_info(struct pipe_inode_i
  {
        int i;
  
-       (void) account_pipe_buffers(pipe->user, pipe->ring_size, 0);
+ #ifdef CONFIG_WATCH_QUEUE
+       if (pipe->watch_queue) {
+               watch_queue_clear(pipe->watch_queue);
+               put_watch_queue(pipe->watch_queue);
+       }
+ #endif
+       (void) account_pipe_buffers(pipe->user, pipe->nr_accounted, 0);
        free_uid(pipe->user);
        for (i = 0; i < pipe->ring_size; i++) {
                struct pipe_buffer *buf = pipe->bufs + i;
@@@ -852,6 -971,17 +917,17 @@@ int create_pipe_files(struct file **res
        if (!inode)
                return -ENFILE;
  
+       if (flags & O_NOTIFICATION_PIPE) {
+ #ifdef CONFIG_WATCH_QUEUE
+               if (watch_queue_init(inode->i_pipe) < 0) {
+                       iput(inode);
+                       return -ENOMEM;
+               }
+ #else
+               return -ENOPKG;
+ #endif
+       }
        f = alloc_file_pseudo(inode, pipe_mnt, "",
                                O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
                                &pipefifo_fops);
@@@ -882,7 -1012,7 +958,7 @@@ static int __do_pipe_flags(int *fd, str
        int error;
        int fdw, fdr;
  
-       if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
+       if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT | O_NOTIFICATION_PIPE))
                return -EINVAL;
  
        error = create_pipe_files(files, flags);
@@@ -1130,42 -1260,12 +1206,12 @@@ unsigned int round_pipe_size(unsigned l
  }
  
  /*
-  * Allocate a new array of pipe buffers and copy the info over. Returns the
-  * pipe size if successful, or return -ERROR on error.
+  * Resize the pipe ring to a number of slots.
   */
static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots)
  {
        struct pipe_buffer *bufs;
-       unsigned int size, nr_slots, head, tail, mask, n;
-       unsigned long user_bufs;
-       long ret = 0;
-       size = round_pipe_size(arg);
-       nr_slots = size >> PAGE_SHIFT;
-       if (!nr_slots)
-               return -EINVAL;
-       /*
-        * If trying to increase the pipe capacity, check that an
-        * unprivileged user is not trying to exceed various limits
-        * (soft limit check here, hard limit check just below).
-        * Decreasing the pipe capacity is always permitted, even
-        * if the user is currently over a limit.
-        */
-       if (nr_slots > pipe->ring_size &&
-                       size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
-               return -EPERM;
-       user_bufs = account_pipe_buffers(pipe->user, pipe->ring_size, nr_slots);
-       if (nr_slots > pipe->ring_size &&
-                       (too_many_pipe_buffers_hard(user_bufs) ||
-                        too_many_pipe_buffers_soft(user_bufs)) &&
-                       is_unprivileged_user()) {
-               ret = -EPERM;
-               goto out_revert_acct;
-       }
+       unsigned int head, tail, mask, n;
  
        /*
         * We can shrink the pipe, if arg is greater than the ring occupancy.
        head = pipe->head;
        tail = pipe->tail;
        n = pipe_occupancy(pipe->head, pipe->tail);
-       if (nr_slots < n) {
-               ret = -EBUSY;
-               goto out_revert_acct;
-       }
+       if (nr_slots < n)
+               return -EBUSY;
  
        bufs = kcalloc(nr_slots, sizeof(*bufs),
                       GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
-       if (unlikely(!bufs)) {
-               ret = -ENOMEM;
-               goto out_revert_acct;
-       }
+       if (unlikely(!bufs))
+               return -ENOMEM;
  
        /*
         * The pipe array wraps around, so just start the new one at zero
        kfree(pipe->bufs);
        pipe->bufs = bufs;
        pipe->ring_size = nr_slots;
-       pipe->max_usage = nr_slots;
+       if (pipe->max_usage > nr_slots)
+               pipe->max_usage = nr_slots;
        pipe->tail = tail;
        pipe->head = head;
  
        /* This might have made more room for writers */
        wake_up_interruptible(&pipe->wr_wait);
+       return 0;
+ }
+ /*
+  * Allocate a new array of pipe buffers and copy the info over. Returns the
+  * pipe size if successful, or return -ERROR on error.
+  */
+ static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg)
+ {
+       unsigned long user_bufs;
+       unsigned int nr_slots, size;
+       long ret = 0;
+ #ifdef CONFIG_WATCH_QUEUE
+       if (pipe->watch_queue)
+               return -EBUSY;
+ #endif
+       size = round_pipe_size(arg);
+       nr_slots = size >> PAGE_SHIFT;
+       if (!nr_slots)
+               return -EINVAL;
+       /*
+        * If trying to increase the pipe capacity, check that an
+        * unprivileged user is not trying to exceed various limits
+        * (soft limit check here, hard limit check just below).
+        * Decreasing the pipe capacity is always permitted, even
+        * if the user is currently over a limit.
+        */
+       if (nr_slots > pipe->max_usage &&
+                       size > pipe_max_size && !capable(CAP_SYS_RESOURCE))
+               return -EPERM;
+       user_bufs = account_pipe_buffers(pipe->user, pipe->nr_accounted, nr_slots);
+       if (nr_slots > pipe->max_usage &&
+                       (too_many_pipe_buffers_hard(user_bufs) ||
+                        too_many_pipe_buffers_soft(user_bufs)) &&
+                       pipe_is_unprivileged_user()) {
+               ret = -EPERM;
+               goto out_revert_acct;
+       }
+       ret = pipe_resize_ring(pipe, nr_slots);
+       if (ret < 0)
+               goto out_revert_acct;
+       pipe->max_usage = nr_slots;
+       pipe->nr_accounted = nr_slots;
        return pipe->max_usage * PAGE_SIZE;
  
  out_revert_acct:
-       (void) account_pipe_buffers(pipe->user, nr_slots, pipe->ring_size);
+       (void) account_pipe_buffers(pipe->user, nr_slots, pipe->nr_accounted);
        return ret;
  }
  
   * location, so checking ->i_pipe is not enough to verify that this is a
   * pipe.
   */
- struct pipe_inode_info *get_pipe_info(struct file *file)
+ struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice)
  {
-       return file->f_op == &pipefifo_fops ? file->private_data : NULL;
+       struct pipe_inode_info *pipe = file->private_data;
+       if (file->f_op != &pipefifo_fops || !pipe)
+               return NULL;
+ #ifdef CONFIG_WATCH_QUEUE
+       if (for_splice && pipe->watch_queue)
+               return NULL;
+ #endif
+       return pipe;
  }
  
  long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
        struct pipe_inode_info *pipe;
        long ret;
  
-       pipe = get_pipe_info(file);
+       pipe = get_pipe_info(file, false);
        if (!pipe)
                return -EBADF;
  
diff --combined fs/splice.c
@@@ -44,8 -44,8 +44,8 @@@
   * addition of remove_mapping(). If success is returned, the caller may
   * attempt to reuse this page for another destination.
   */
 -static int page_cache_pipe_buf_steal(struct pipe_inode_info *pipe,
 -                                   struct pipe_buffer *buf)
 +static bool page_cache_pipe_buf_try_steal(struct pipe_inode_info *pipe,
 +              struct pipe_buffer *buf)
  {
        struct page *page = buf->page;
        struct address_space *mapping;
@@@ -76,7 -76,7 +76,7 @@@
                 */
                if (remove_mapping(mapping, page)) {
                        buf->flags |= PIPE_BUF_FLAG_LRU;
 -                      return 0;
 +                      return true;
                }
        }
  
@@@ -86,7 -86,7 +86,7 @@@
         */
  out_unlock:
        unlock_page(page);
 -      return 1;
 +      return false;
  }
  
  static void page_cache_pipe_buf_release(struct pipe_inode_info *pipe,
@@@ -139,26 -139,27 +139,26 @@@ error
  }
  
  const struct pipe_buf_operations page_cache_pipe_buf_ops = {
 -      .confirm = page_cache_pipe_buf_confirm,
 -      .release = page_cache_pipe_buf_release,
 -      .steal = page_cache_pipe_buf_steal,
 -      .get = generic_pipe_buf_get,
 +      .confirm        = page_cache_pipe_buf_confirm,
 +      .release        = page_cache_pipe_buf_release,
 +      .try_steal      = page_cache_pipe_buf_try_steal,
 +      .get            = generic_pipe_buf_get,
  };
  
 -static int user_page_pipe_buf_steal(struct pipe_inode_info *pipe,
 -                                  struct pipe_buffer *buf)
 +static bool user_page_pipe_buf_try_steal(struct pipe_inode_info *pipe,
 +              struct pipe_buffer *buf)
  {
        if (!(buf->flags & PIPE_BUF_FLAG_GIFT))
 -              return 1;
 +              return false;
  
        buf->flags |= PIPE_BUF_FLAG_LRU;
 -      return generic_pipe_buf_steal(pipe, buf);
 +      return generic_pipe_buf_try_steal(pipe, buf);
  }
  
  static const struct pipe_buf_operations user_page_pipe_buf_ops = {
 -      .confirm = generic_pipe_buf_confirm,
 -      .release = page_cache_pipe_buf_release,
 -      .steal = user_page_pipe_buf_steal,
 -      .get = generic_pipe_buf_get,
 +      .release        = page_cache_pipe_buf_release,
 +      .try_steal      = user_page_pipe_buf_try_steal,
 +      .get            = generic_pipe_buf_get,
  };
  
  static void wakeup_pipe_readers(struct pipe_inode_info *pipe)
@@@ -330,15 -331,24 +330,15 @@@ ssize_t generic_file_splice_read(struc
  EXPORT_SYMBOL(generic_file_splice_read);
  
  const struct pipe_buf_operations default_pipe_buf_ops = {
 -      .confirm = generic_pipe_buf_confirm,
 -      .release = generic_pipe_buf_release,
 -      .steal = generic_pipe_buf_steal,
 -      .get = generic_pipe_buf_get,
 +      .release        = generic_pipe_buf_release,
 +      .try_steal      = generic_pipe_buf_try_steal,
 +      .get            = generic_pipe_buf_get,
  };
  
 -int generic_pipe_buf_nosteal(struct pipe_inode_info *pipe,
 -                           struct pipe_buffer *buf)
 -{
 -      return 1;
 -}
 -
  /* Pipe buffer operations for a socket and similar. */
  const struct pipe_buf_operations nosteal_pipe_buf_ops = {
 -      .confirm = generic_pipe_buf_confirm,
 -      .release = generic_pipe_buf_release,
 -      .steal = generic_pipe_buf_nosteal,
 -      .get = generic_pipe_buf_get,
 +      .release        = generic_pipe_buf_release,
 +      .get            = generic_pipe_buf_get,
  };
  EXPORT_SYMBOL(nosteal_pipe_buf_ops);
  
@@@ -842,9 -852,15 +842,9 @@@ EXPORT_SYMBOL(generic_splice_sendpage)
  static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
                           loff_t *ppos, size_t len, unsigned int flags)
  {
 -      ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
 -                              loff_t *, size_t, unsigned int);
 -
        if (out->f_op->splice_write)
 -              splice_write = out->f_op->splice_write;
 -      else
 -              splice_write = default_file_splice_write;
 -
 -      return splice_write(pipe, out, ppos, len, flags);
 +              return out->f_op->splice_write(pipe, out, ppos, len, flags);
 +      return default_file_splice_write(pipe, out, ppos, len, flags);
  }
  
  /*
@@@ -854,6 -870,8 +854,6 @@@ static long do_splice_to(struct file *i
                         struct pipe_inode_info *pipe, size_t len,
                         unsigned int flags)
  {
 -      ssize_t (*splice_read)(struct file *, loff_t *,
 -                             struct pipe_inode_info *, size_t, unsigned int);
        int ret;
  
        if (unlikely(!(in->f_mode & FMODE_READ)))
                len = MAX_RW_COUNT;
  
        if (in->f_op->splice_read)
 -              splice_read = in->f_op->splice_read;
 -      else
 -              splice_read = default_file_splice_read;
 -
 -      return splice_read(in, ppos, pipe, len, flags);
 +              return in->f_op->splice_read(in, ppos, pipe, len, flags);
 +      return default_file_splice_read(in, ppos, pipe, len, flags);
  }
  
  /**
@@@ -1101,8 -1122,8 +1101,8 @@@ long do_splice(struct file *in, loff_t 
                     !(out->f_mode & FMODE_WRITE)))
                return -EBADF;
  
-       ipipe = get_pipe_info(in);
-       opipe = get_pipe_info(out);
+       ipipe = get_pipe_info(in, true);
+       opipe = get_pipe_info(out, true);
  
        if (ipipe && opipe) {
                if (off_in || off_out)
@@@ -1252,7 -1273,7 +1252,7 @@@ static int pipe_to_user(struct pipe_ino
  static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
                             unsigned int flags)
  {
-       struct pipe_inode_info *pipe = get_pipe_info(file);
+       struct pipe_inode_info *pipe = get_pipe_info(file, true);
        struct splice_desc sd = {
                .total_len = iov_iter_count(iter),
                .flags = flags,
@@@ -1287,7 -1308,7 +1287,7 @@@ static long vmsplice_to_pipe(struct fil
        if (flags & SPLICE_F_GIFT)
                buf_flag = PIPE_BUF_FLAG_GIFT;
  
-       pipe = get_pipe_info(file);
+       pipe = get_pipe_info(file, true);
        if (!pipe)
                return -EBADF;
  
@@@ -1473,7 -1494,7 +1473,7 @@@ static int opipe_prep(struct pipe_inode
         * Check pipe occupancy without the inode lock first. This function
         * is speculative anyways, so missing one is ok.
         */
 -      if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
 +      if (!pipe_full(pipe->head, pipe->tail, pipe->max_usage))
                return 0;
  
        ret = 0;
@@@ -1605,11 -1626,12 +1605,11 @@@ retry
                        *obuf = *ibuf;
  
                        /*
 -                       * Don't inherit the gift flag, we need to
 +                       * Don't inherit the gift and merge flags, we need to
                         * prevent multiple steals of this page.
                         */
                        obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
 -
 -                      pipe_buf_mark_unmergeable(obuf);
 +                      obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
  
                        obuf->len = len;
                        ibuf->offset += len;
@@@ -1697,11 -1719,12 +1697,11 @@@ static int link_pipe(struct pipe_inode_
                *obuf = *ibuf;
  
                /*
 -               * Don't inherit the gift flag, we need to
 -               * prevent multiple steals of this page.
 +               * Don't inherit the gift and merge flag, we need to prevent
 +               * multiple steals of this page.
                 */
                obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
 -
 -              pipe_buf_mark_unmergeable(obuf);
 +              obuf->flags &= ~PIPE_BUF_FLAG_CAN_MERGE;
  
                if (obuf->len > len)
                        obuf->len = len;
   * The 'flags' used are the SPLICE_F_* variants, currently the only
   * applicable one is SPLICE_F_NONBLOCK.
   */
 -static long do_tee(struct file *in, struct file *out, size_t len,
 -                 unsigned int flags)
 +long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
  {
-       struct pipe_inode_info *ipipe = get_pipe_info(in);
-       struct pipe_inode_info *opipe = get_pipe_info(out);
+       struct pipe_inode_info *ipipe = get_pipe_info(in, true);
+       struct pipe_inode_info *opipe = get_pipe_info(out, true);
        int ret = -EINVAL;
  
        if (unlikely(!(in->f_mode & FMODE_READ) ||
@@@ -49,8 -49,7 +49,8 @@@ LSM_HOOK(int, 0, syslog, int type
  LSM_HOOK(int, 0, settime, const struct timespec64 *ts,
         const struct timezone *tz)
  LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages)
 -LSM_HOOK(int, 0, bprm_set_creds, struct linux_binprm *bprm)
 +LSM_HOOK(int, 0, bprm_creds_for_exec, struct linux_binprm *bprm)
 +LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, struct file *file)
  LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
  LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, struct linux_binprm *bprm)
  LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, struct linux_binprm *bprm)
@@@ -254,6 -253,15 +254,15 @@@ LSM_HOOK(int, 0, inode_setsecctx, struc
  LSM_HOOK(int, 0, inode_getsecctx, struct inode *inode, void **ctx,
         u32 *ctxlen)
  
+ #if defined(CONFIG_SECURITY) && defined(CONFIG_WATCH_QUEUE)
+ LSM_HOOK(int, 0, post_notification, const struct cred *w_cred,
+        const struct cred *cred, struct watch_notification *n)
+ #endif /* CONFIG_SECURITY && CONFIG_WATCH_QUEUE */
+ #if defined(CONFIG_SECURITY) && defined(CONFIG_KEY_NOTIFICATIONS)
+ LSM_HOOK(int, 0, watch_key, struct key *key)
+ #endif /* CONFIG_SECURITY && CONFIG_KEY_NOTIFICATIONS */
  #ifdef CONFIG_SECURITY_NETWORK
  LSM_HOOK(int, 0, unix_stream_connect, struct sock *sock, struct sock *other,
         struct sock *newsk)
   *
   * Security hooks for program execution operations.
   *
 - * @bprm_set_creds:
 - *    Save security information in the bprm->security field, typically based
 - *    on information about the bprm->file, for later use by the apply_creds
 - *    hook.  This hook may also optionally check permissions (e.g. for
 - *    transitions between security domains).
 - *    This hook may be called multiple times during a single execve, e.g. for
 - *    interpreters.  The hook can tell whether it has already been called by
 - *    checking to see if @bprm->security is non-NULL.  If so, then the hook
 - *    may decide either to retain the security information saved earlier or
 - *    to replace it.  The hook must set @bprm->secureexec to 1 if a "secure
 - *    exec" has happened as a result of this hook call.  The flag is used to
 - *    indicate the need for a sanitized execution environment, and is also
 - *    passed in the ELF auxiliary table on the initial stack to indicate
 - *    whether libc should enable secure mode.
 + * @bprm_creds_for_exec:
 + *    If the setup in prepare_exec_creds did not setup @bprm->cred->security
 + *    properly for executing @bprm->file, update the LSM's portion of
 + *    @bprm->cred->security to be what commit_creds needs to install for the
 + *    new program.  This hook may also optionally check permissions
 + *    (e.g. for transitions between security domains).
 + *    The hook must set @bprm->secureexec to 1 if AT_SECURE should be set to
 + *    request libc enable secure mode.
 + *    @bprm contains the linux_binprm structure.
 + *    Return 0 if the hook is successful and permission is granted.
 + * @bprm_creds_from_file:
 + *    If @file is setpcap, suid, sgid or otherwise marked to change
 + *    privilege upon exec, update @bprm->cred to reflect that change.
 + *    This is called after finding the binary that will be executed.
 + *    without an interpreter.  This ensures that the credentials will not
 + *    be derived from a script that the binary will need to reopen, which
 + *    when reopend may end up being a completely different file.  This
 + *    hook may also optionally check permissions (e.g. for transitions
 + *    between security domains).
 + *    The hook must set @bprm->secureexec to 1 if AT_SECURE should be set to
 + *    request libc enable secure mode.
 + *    The hook must add to @bprm->per_clear any personality flags that
 + *    should be cleared from current->personality.
   *    @bprm contains the linux_binprm structure.
   *    Return 0 if the hook is successful and permission is granted.
   * @bprm_check_security:
   *    This hook mediates the point when a search for a binary handler will
 - *    begin.  It allows a check the @bprm->security value which is set in the
 - *    preceding set_creds call.  The primary difference from set_creds is
 - *    that the argv list and envp list are reliably available in @bprm.  This
 - *    hook may be called multiple times during a single execve; and in each
 - *    pass set_creds is called first.
 + *    begin.  It allows a check against the @bprm->cred->security value
 + *    which was set in the preceding creds_for_exec call.  The argv list and
 + *    envp list are reliably available in @bprm.  This hook may be called
 + *    multiple times during a single execve.
   *    @bprm contains the linux_binprm structure.
   *    Return 0 if the hook is successful and permission is granted.
   * @bprm_committing_creds:
   *    Prepare to install the new security attributes of a process being
   *    transformed by an execve operation, based on the old credentials
   *    pointed to by @current->cred and the information set in @bprm->cred by
 - *    the bprm_set_creds hook.  @bprm points to the linux_binprm structure.
 - *    This hook is a good place to perform state changes on the process such
 - *    as closing open file descriptors to which access will no longer be
 - *    granted when the attributes are changed.  This is called immediately
 - *    before commit_creds().
 + *    the bprm_creds_for_exec hook.  @bprm points to the linux_binprm
 + *    structure.  This hook is a good place to perform state changes on the
 + *    process such as closing open file descriptors to which access will no
 + *    longer be granted when the attributes are changed.  This is called
 + *    immediately before commit_creds().
   * @bprm_committed_creds:
   *    Tidy up after the installation of the new security attributes of a
   *    process being transformed by an execve operation.  The new credentials
@@@ -85,7 -77,7 +85,7 @@@
   *    state.  This is called immediately after commit_creds().
   *
   * Security hooks for mount using fs_context.
 - *    [See also Documentation/filesystems/mount_api.txt]
 + *    [See also Documentation/filesystems/mount_api.rst]
   *
   * @fs_context_dup:
   *    Allocate and attach a security structure to sc->security.  This pointer
   *    @ctx is a pointer in which to place the allocated security context.
   *    @ctxlen points to the place to put the length of @ctx.
   *
+  * Security hooks for the general notification queue:
+  *
+  * @post_notification:
+  *    Check to see if a watch notification can be posted to a particular
+  *    queue.
+  *    @w_cred: The credentials of the whoever set the watch.
+  *    @cred: The event-triggerer's credentials
+  *    @n: The notification being posted
+  *
+  * @watch_key:
+  *    Check to see if a process is allowed to watch for event notifications
+  *    from a key or keyring.
+  *    @key: The key to watch.
+  *
   * Security hooks for using the eBPF maps and programs functionalities through
   * eBPF syscalls.
   *
@@@ -8,7 -8,10 +8,11 @@@
  #define PIPE_BUF_FLAG_ATOMIC  0x02    /* was atomically mapped */
  #define PIPE_BUF_FLAG_GIFT    0x04    /* page is a gift */
  #define PIPE_BUF_FLAG_PACKET  0x08    /* read() as a packet */
 -#define PIPE_BUF_FLAG_WHOLE   0x10    /* read() must return entire buffer or error */
 +#define PIPE_BUF_FLAG_CAN_MERGE       0x10    /* can merge buffers */
++#define PIPE_BUF_FLAG_WHOLE   0x20    /* read() must return entire buffer or error */
+ #ifdef CONFIG_WATCH_QUEUE
 -#define PIPE_BUF_FLAG_LOSS    0x20    /* Message loss happened after this buffer */
++#define PIPE_BUF_FLAG_LOSS    0x40    /* Message loss happened after this buffer */
+ #endif
  
  /**
   *    struct pipe_buffer - a linux kernel pipe buffer
@@@ -34,8 -37,10 +38,10 @@@ struct pipe_buffer 
   *    @wr_wait: writer wait point in case of full pipe
   *    @head: The point of buffer production
   *    @tail: The point of buffer consumption
+  *    @note_loss: The next read() should insert a data-lost message
   *    @max_usage: The maximum number of slots that may be used in the ring
   *    @ring_size: total number of buffers (should be a power of 2)
+  *    @nr_accounted: The amount this pipe accounts for in user->pipe_bufs
   *    @tmp_page: cached released page
   *    @readers: number of current readers of this pipe
   *    @writers: number of current writers of this pipe
@@@ -46,6 -51,7 +52,7 @@@
   *    @fasync_writers: writer side fasync
   *    @bufs: the circular array of pipe buffers
   *    @user: the user who created this pipe
+  *    @watch_queue: If this pipe is a watch_queue, this is the stuff for that
   **/
  struct pipe_inode_info {
        struct mutex mutex;
        unsigned int tail;
        unsigned int max_usage;
        unsigned int ring_size;
+ #ifdef CONFIG_WATCH_QUEUE
+       bool note_loss;
+ #endif
+       unsigned int nr_accounted;
        unsigned int readers;
        unsigned int writers;
        unsigned int files;
        struct fasync_struct *fasync_writers;
        struct pipe_buffer *bufs;
        struct user_struct *user;
+ #ifdef CONFIG_WATCH_QUEUE
+       struct watch_queue *watch_queue;
+ #endif
  };
  
  /*
   * Note on the nesting of these functions:
   *
   * ->confirm()
 - *    ->steal()
 + *    ->try_steal()
   *
 - * That is, ->steal() must be called on a confirmed buffer.
 - * See below for the meaning of each operation. Also see kerneldoc
 - * in fs/pipe.c for the pipe and generic variants of these hooks.
 + * That is, ->try_steal() must be called on a confirmed buffer.  See below for
 + * the meaning of each operation.  Also see the kerneldoc in fs/pipe.c for the
 + * pipe and generic variants of these hooks.
   */
  struct pipe_buf_operations {
        /*
@@@ -82,7 -95,7 +96,7 @@@
         * and that the contents are good. If the pages in the pipe belong
         * to a file system, we may need to wait for IO completion in this
         * hook. Returns 0 for good, or a negative error value in case of
 -       * error.
 +       * error.  If not present all pages are considered good.
         */
        int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *);
  
  
        /*
         * Attempt to take ownership of the pipe buffer and its contents.
 -       * ->steal() returns 0 for success, in which case the contents
 -       * of the pipe (the buf->page) is locked and now completely owned
 -       * by the caller. The page may then be transferred to a different
 -       * mapping, the most often used case is insertion into different
 -       * file address space cache.
 +       * ->try_steal() returns %true for success, in which case the contents
 +       * of the pipe (the buf->page) is locked and now completely owned by the
 +       * caller. The page may then be transferred to a different mapping, the
 +       * most often used case is insertion into different file address space
 +       * cache.
         */
 -      int (*steal)(struct pipe_inode_info *, struct pipe_buffer *);
 +      bool (*try_steal)(struct pipe_inode_info *, struct pipe_buffer *);
  
        /*
         * Get a reference to the pipe buffer.
@@@ -195,22 -208,18 +209,22 @@@ static inline void pipe_buf_release(str
  static inline int pipe_buf_confirm(struct pipe_inode_info *pipe,
                                   struct pipe_buffer *buf)
  {
 +      if (!buf->ops->confirm)
 +              return 0;
        return buf->ops->confirm(pipe, buf);
  }
  
  /**
 - * pipe_buf_steal - attempt to take ownership of a pipe_buffer
 + * pipe_buf_try_steal - attempt to take ownership of a pipe_buffer
   * @pipe:     the pipe that the buffer belongs to
   * @buf:      the buffer to attempt to steal
   */
 -static inline int pipe_buf_steal(struct pipe_inode_info *pipe,
 -                               struct pipe_buffer *buf)
 +static inline bool pipe_buf_try_steal(struct pipe_inode_info *pipe,
 +              struct pipe_buffer *buf)
  {
 -      return buf->ops->steal(pipe, buf);
 +      if (!buf->ops->try_steal)
 +              return false;
 +      return buf->ops->try_steal(pipe, buf);
  }
  
  /* Differs from PIPE_BUF in that PIPE_SIZE is the length of the actual
@@@ -234,14 -243,28 +248,25 @@@ void free_pipe_info(struct pipe_inode_i
  
  /* Generic pipe buffer ops functions */
  bool generic_pipe_buf_get(struct pipe_inode_info *, struct pipe_buffer *);
 -int generic_pipe_buf_confirm(struct pipe_inode_info *, struct pipe_buffer *);
 -int generic_pipe_buf_steal(struct pipe_inode_info *, struct pipe_buffer *);
 -int generic_pipe_buf_nosteal(struct pipe_inode_info *, struct pipe_buffer *);
 +bool generic_pipe_buf_try_steal(struct pipe_inode_info *, struct pipe_buffer *);
  void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *);
 -void pipe_buf_mark_unmergeable(struct pipe_buffer *buf);
  
  extern const struct pipe_buf_operations nosteal_pipe_buf_ops;
  
+ #ifdef CONFIG_WATCH_QUEUE
+ unsigned long account_pipe_buffers(struct user_struct *user,
+                                  unsigned long old, unsigned long new);
+ bool too_many_pipe_buffers_soft(unsigned long user_bufs);
+ bool too_many_pipe_buffers_hard(unsigned long user_bufs);
+ bool pipe_is_unprivileged_user(void);
+ #endif
  /* for F_SETPIPE_SZ and F_GETPIPE_SZ */
+ #ifdef CONFIG_WATCH_QUEUE
+ int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots);
+ #endif
  long pipe_fcntl(struct file *, unsigned int, unsigned long arg);
- struct pipe_inode_info *get_pipe_info(struct file *file);
+ struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice);
  
  int create_pipe_files(struct file **, int);
  unsigned int round_pipe_size(unsigned long size);
diff --combined include/linux/security.h
@@@ -56,6 -56,8 +56,8 @@@ struct mm_struct
  struct fs_context;
  struct fs_parameter;
  enum fs_value_type;
+ struct watch;
+ struct watch_notification;
  
  /* Default (no) options for the capable function */
  #define CAP_OPT_NONE 0x0
@@@ -140,7 -142,7 +142,7 @@@ extern int cap_capset(struct cred *new
                      const kernel_cap_t *effective,
                      const kernel_cap_t *inheritable,
                      const kernel_cap_t *permitted);
 -extern int cap_bprm_set_creds(struct linux_binprm *bprm);
 +extern int cap_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
  extern int cap_inode_setxattr(struct dentry *dentry, const char *name,
                              const void *value, size_t size, int flags);
  extern int cap_inode_removexattr(struct dentry *dentry, const char *name);
@@@ -211,7 -213,7 +213,7 @@@ struct request_sock
  
  #ifdef CONFIG_MMU
  extern int mmap_min_addr_handler(struct ctl_table *table, int write,
 -                               void __user *buffer, size_t *lenp, loff_t *ppos);
 +                               void *buffer, size_t *lenp, loff_t *ppos);
  #endif
  
  /* security_inode_init_security callback function to write xattrs */
@@@ -276,8 -278,7 +278,8 @@@ int security_quota_on(struct dentry *de
  int security_syslog(int type);
  int security_settime64(const struct timespec64 *ts, const struct timezone *tz);
  int security_vm_enough_memory_mm(struct mm_struct *mm, long pages);
 -int security_bprm_set_creds(struct linux_binprm *bprm);
 +int security_bprm_creds_for_exec(struct linux_binprm *bprm);
 +int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file);
  int security_bprm_check(struct linux_binprm *bprm);
  void security_bprm_committing_creds(struct linux_binprm *bprm);
  void security_bprm_committed_creds(struct linux_binprm *bprm);
@@@ -570,15 -571,9 +572,15 @@@ static inline int security_vm_enough_me
        return __vm_enough_memory(mm, pages, cap_vm_enough_memory(mm, pages));
  }
  
 -static inline int security_bprm_set_creds(struct linux_binprm *bprm)
 +static inline int security_bprm_creds_for_exec(struct linux_binprm *bprm)
  {
 -      return cap_bprm_set_creds(bprm);
 +      return 0;
 +}
 +
 +static inline int security_bprm_creds_from_file(struct linux_binprm *bprm,
 +                                              struct file *file)
 +{
 +      return cap_bprm_creds_from_file(bprm, file);
  }
  
  static inline int security_bprm_check(struct linux_binprm *bprm)
@@@ -1282,6 -1277,28 +1284,28 @@@ static inline int security_locked_down(
  }
  #endif        /* CONFIG_SECURITY */
  
+ #if defined(CONFIG_SECURITY) && defined(CONFIG_WATCH_QUEUE)
+ int security_post_notification(const struct cred *w_cred,
+                              const struct cred *cred,
+                              struct watch_notification *n);
+ #else
+ static inline int security_post_notification(const struct cred *w_cred,
+                                            const struct cred *cred,
+                                            struct watch_notification *n)
+ {
+       return 0;
+ }
+ #endif
+ #if defined(CONFIG_SECURITY) && defined(CONFIG_KEY_NOTIFICATIONS)
+ int security_watch_key(struct key *key);
+ #else
+ static inline int security_watch_key(struct key *key)
+ {
+       return 0;
+ }
+ #endif
  #ifdef CONFIG_SECURITY_NETWORK
  
  int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk);
@@@ -1750,8 -1767,8 +1774,8 @@@ static inline int security_path_chroot(
  
  int security_key_alloc(struct key *key, const struct cred *cred, unsigned long flags);
  void security_key_free(struct key *key);
- int security_key_permission(key_ref_t key_ref,
-                           const struct cred *cred, unsigned perm);
+ int security_key_permission(key_ref_t key_ref, const struct cred *cred,
+                           enum key_need_perm need_perm);
  int security_key_getsecurity(struct key *key, char **_buffer);
  
  #else
@@@ -1769,7 -1786,7 +1793,7 @@@ static inline void security_key_free(st
  
  static inline int security_key_permission(key_ref_t key_ref,
                                          const struct cred *cred,
-                                         unsigned perm)
+                                         enum key_need_perm need_perm)
  {
        return 0;
  }
diff --combined init/Kconfig
@@@ -8,25 -8,8 +8,25 @@@ config DEFCONFIG_LIS
        default "/boot/config-$(shell,uname -r)"
        default "arch/$(SRCARCH)/configs/$(KBUILD_DEFCONFIG)"
  
 +config CC_VERSION_TEXT
 +      string
 +      default "$(CC_VERSION_TEXT)"
 +      help
 +        This is used in unclear ways:
 +
 +        - Re-run Kconfig when the compiler is updated
 +          The 'default' property references the environment variable,
 +          CC_VERSION_TEXT so it is recorded in include/config/auto.conf.cmd.
 +          When the compiler is updated, Kconfig will be invoked.
 +
 +        - Ensure full rebuild when the compier is updated
 +          include/linux/kconfig.h contains this option in the comment line so
 +          fixdep adds include/config/cc/version/text.h into the auto-generated
 +          dependency. When the compiler is updated, syncconfig will touch it
 +          and then every file will be rebuilt.
 +
  config CC_IS_GCC
 -      def_bool $(success,$(CC) --version | head -n 1 | grep -q gcc)
 +      def_bool $(success,echo "$(CC_VERSION_TEXT)" | grep -q gcc)
  
  config GCC_VERSION
        int
@@@ -38,32 -21,18 +38,32 @@@ config LD_VERSIO
        default $(shell,$(LD) --version | $(srctree)/scripts/ld-version.sh)
  
  config CC_IS_CLANG
 -      def_bool $(success,$(CC) --version | head -n 1 | grep -q clang)
 +      def_bool $(success,echo "$(CC_VERSION_TEXT)" | grep -q clang)
 +
 +config LD_IS_LLD
 +      def_bool $(success,$(LD) -v | head -n 1 | grep -q LLD)
  
  config CLANG_VERSION
        int
        default $(shell,$(srctree)/scripts/clang-version.sh $(CC))
  
  config CC_CAN_LINK
 -      def_bool $(success,$(srctree)/scripts/cc-can-link.sh $(CC))
 +      bool
 +      default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(m64-flag)) if 64BIT
 +      default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(m32-flag))
 +
 +config CC_CAN_LINK_STATIC
 +      bool
 +      default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) -static $(m64-flag)) if 64BIT
 +      default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) -static $(m32-flag))
  
  config CC_HAS_ASM_GOTO
        def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
  
 +config CC_HAS_ASM_GOTO_OUTPUT
 +      depends on CC_HAS_ASM_GOTO
 +      def_bool $(success,echo 'int foo(int x) { asm goto ("": "=r"(x) ::: bar); return x; bar: return 0; }' | $(CC) -x c - -c -o /dev/null)
 +
  config TOOLS_SUPPORT_RELR
        def_bool $(success,env "CC=$(CC)" "LD=$(LD)" "NM=$(NM)" "OBJCOPY=$(OBJCOPY)" $(srctree)/scripts/tools-support-relr.sh)
  
@@@ -288,16 -257,6 +288,16 @@@ config KERNEL_UNCOMPRESSE
  
  endchoice
  
 +config DEFAULT_INIT
 +      string "Default init path"
 +      default ""
 +      help
 +        This option determines the default init for the system if no init=
 +        option is passed on the kernel command line. If the requested path is
 +        not present, we will still then move on to attempting further
 +        locations (e.g. /sbin/init, etc). If this is empty, we will just use
 +        the fallback list when init= is not passed.
 +
  config DEFAULT_HOSTNAME
        string "Default hostname"
        default "(none)"
@@@ -367,6 -326,18 +367,18 @@@ config POSIX_MQUEUE_SYSCT
        depends on SYSCTL
        default y
  
+ config WATCH_QUEUE
+       bool "General notification queue"
+       default n
+       help
+         This is a general notification queue for the kernel to pass events to
+         userspace by splicing them into pipes.  It can be used in conjunction
+         with watches for key/keyring change notifications and device
+         notifications.
+         See Documentation/watch_queue.rst
  config CROSS_MEMORY_ATTACH
        bool "Enable process_vm_readv/writev syscalls"
        depends on MMU
@@@ -860,9 -831,24 +872,9 @@@ config MEMC
          Provides control over the memory footprint of tasks in a cgroup.
  
  config MEMCG_SWAP
 -      bool "Swap controller"
 +      bool
        depends on MEMCG && SWAP
 -      help
 -        Provides control over the swap space consumed by tasks in a cgroup.
 -
 -config MEMCG_SWAP_ENABLED
 -      bool "Swap controller enabled by default"
 -      depends on MEMCG_SWAP
        default y
 -      help
 -        Memory Resource Controller Swap Extension comes with its price in
 -        a bigger memory consumption. General purpose distribution kernels
 -        which want to enable the feature but keep it disabled by default
 -        and let the user enable it by swapaccount=1 boot command line
 -        parameter should have this option unselected.
 -        For those who want to have the feature enabled by default should
 -        select this option (if, for some reason, they need to disable it
 -        then swapaccount=0 does the trick).
  
  config MEMCG_KMEM
        bool
@@@ -1293,6 -1279,7 +1305,6 @@@ config LD_DEAD_CODE_DATA_ELIMINATIO
        bool "Dead code and data elimination (EXPERIMENTAL)"
        depends on HAVE_LD_DEAD_CODE_DATA_ELIMINATION
        depends on EXPERT
 -      depends on !(FUNCTION_TRACER && CC_IS_GCC && GCC_VERSION < 40800)
        depends on $(cc-option,-ffunction-sections -fdata-sections)
        depends on $(ld-option,--gc-sections)
        help
diff --combined kernel/Makefile
@@@ -23,9 -23,6 +23,9 @@@ endi
  # Prevents flicker of uninteresting __do_softirq()/__local_bh_disable_ip()
  # in coverage traces.
  KCOV_INSTRUMENT_softirq.o := n
 +# Avoid KCSAN instrumentation in softirq ("No shared variables, all the data
 +# are CPU local" => assume no data races), to reduce overhead in interrupts.
 +KCSAN_SANITIZE_softirq.o = n
  # These are called from save_stack_trace() on slub debug path,
  # and produce insane amounts of uninteresting coverage.
  KCOV_INSTRUMENT_module.o := n
@@@ -34,7 -31,6 +34,7 @@@ KCOV_INSTRUMENT_stacktrace.o := 
  # Don't self-instrument.
  KCOV_INSTRUMENT_kcov.o := n
  KASAN_SANITIZE_kcov.o := n
 +KCSAN_SANITIZE_kcov.o := n
  CFLAGS_kcov.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
  
  # cond_syscall is currently not LTO compatible
@@@ -107,8 -103,6 +107,8 @@@ obj-$(CONFIG_TRACEPOINTS) += trace
  obj-$(CONFIG_IRQ_WORK) += irq_work.o
  obj-$(CONFIG_CPU_PM) += cpu_pm.o
  obj-$(CONFIG_BPF) += bpf/
 +obj-$(CONFIG_KCSAN) += kcsan/
 +obj-$(CONFIG_SHADOW_CALL_STACK) += scs.o
  
  obj-$(CONFIG_PERF_EVENTS) += events/
  
@@@ -121,12 -115,12 +121,13 @@@ obj-$(CONFIG_TORTURE_TEST) += torture.
  
  obj-$(CONFIG_HAS_IOMEM) += iomem.o
  obj-$(CONFIG_RSEQ) += rseq.o
+ obj-$(CONFIG_WATCH_QUEUE) += watch_queue.o
  
  obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o
  
  obj-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak.o
  KASAN_SANITIZE_stackleak.o := n
 +KCSAN_SANITIZE_stackleak.o := n
  KCOV_INSTRUMENT_stackleak.o := n
  
  $(obj)/configs.o: $(obj)/config_data.gz
diff --combined kernel/watch_queue.c
index 0000000,9a9699c..f74020f
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,659 +1,655 @@@
 -static int watch_queue_pipe_buf_steal(struct pipe_inode_info *pipe,
 -                                    struct pipe_buffer *buf)
 -{
 -      return -1; /* No. */
 -}
+ // SPDX-License-Identifier: GPL-2.0
+ /* Watch queue and general notification mechanism, built on pipes
+  *
+  * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
+  * Written by David Howells (dhowells@redhat.com)
+  *
+  * See Documentation/watch_queue.rst
+  */
+ #define pr_fmt(fmt) "watchq: " fmt
+ #include <linux/module.h>
+ #include <linux/init.h>
+ #include <linux/sched.h>
+ #include <linux/slab.h>
+ #include <linux/printk.h>
+ #include <linux/miscdevice.h>
+ #include <linux/fs.h>
+ #include <linux/mm.h>
+ #include <linux/pagemap.h>
+ #include <linux/poll.h>
+ #include <linux/uaccess.h>
+ #include <linux/vmalloc.h>
+ #include <linux/file.h>
+ #include <linux/security.h>
+ #include <linux/cred.h>
+ #include <linux/sched/signal.h>
+ #include <linux/watch_queue.h>
+ #include <linux/pipe_fs_i.h>
+ MODULE_DESCRIPTION("Watch queue");
+ MODULE_AUTHOR("Red Hat, Inc.");
+ MODULE_LICENSE("GPL");
+ #define WATCH_QUEUE_NOTE_SIZE 128
+ #define WATCH_QUEUE_NOTES_PER_PAGE (PAGE_SIZE / WATCH_QUEUE_NOTE_SIZE)
+ static void watch_queue_pipe_buf_release(struct pipe_inode_info *pipe,
+                                        struct pipe_buffer *buf)
+ {
+       struct watch_queue *wqueue = (struct watch_queue *)buf->private;
+       struct page *page;
+       unsigned int bit;
+       /* We need to work out which note within the page this refers to, but
+        * the note might have been maximum size, so merely ANDing the offset
+        * off doesn't work.  OTOH, the note must've been more than zero size.
+        */
+       bit = buf->offset + buf->len;
+       if ((bit & (WATCH_QUEUE_NOTE_SIZE - 1)) == 0)
+               bit -= WATCH_QUEUE_NOTE_SIZE;
+       bit /= WATCH_QUEUE_NOTE_SIZE;
+       page = buf->page;
+       bit += page->index;
+       set_bit(bit, wqueue->notes_bitmap);
+ }
 -      .confirm        = generic_pipe_buf_confirm,
++// No try_steal function => no stealing
++#define watch_queue_pipe_buf_try_steal NULL
+ /* New data written to a pipe may be appended to a buffer with this type. */
+ static const struct pipe_buf_operations watch_queue_pipe_buf_ops = {
 -      .steal          = watch_queue_pipe_buf_steal,
+       .release        = watch_queue_pipe_buf_release,
++      .try_steal      = watch_queue_pipe_buf_try_steal,
+       .get            = generic_pipe_buf_get,
+ };
+ /*
+  * Post a notification to a watch queue.
+  */
+ static bool post_one_notification(struct watch_queue *wqueue,
+                                 struct watch_notification *n)
+ {
+       void *p;
+       struct pipe_inode_info *pipe = wqueue->pipe;
+       struct pipe_buffer *buf;
+       struct page *page;
+       unsigned int head, tail, mask, note, offset, len;
+       bool done = false;
+       if (!pipe)
+               return false;
+       spin_lock_irq(&pipe->rd_wait.lock);
+       if (wqueue->defunct)
+               goto out;
+       mask = pipe->ring_size - 1;
+       head = pipe->head;
+       tail = pipe->tail;
+       if (pipe_full(head, tail, pipe->ring_size))
+               goto lost;
+       note = find_first_bit(wqueue->notes_bitmap, wqueue->nr_notes);
+       if (note >= wqueue->nr_notes)
+               goto lost;
+       page = wqueue->notes[note / WATCH_QUEUE_NOTES_PER_PAGE];
+       offset = note % WATCH_QUEUE_NOTES_PER_PAGE * WATCH_QUEUE_NOTE_SIZE;
+       get_page(page);
+       len = n->info & WATCH_INFO_LENGTH;
+       p = kmap_atomic(page);
+       memcpy(p + offset, n, len);
+       kunmap_atomic(p);
+       buf = &pipe->bufs[head & mask];
+       buf->page = page;
+       buf->private = (unsigned long)wqueue;
+       buf->ops = &watch_queue_pipe_buf_ops;
+       buf->offset = offset;
+       buf->len = len;
+       buf->flags = PIPE_BUF_FLAG_WHOLE;
+       pipe->head = head + 1;
+       if (!test_and_clear_bit(note, wqueue->notes_bitmap)) {
+               spin_unlock_irq(&pipe->rd_wait.lock);
+               BUG();
+       }
+       wake_up_interruptible_sync_poll_locked(&pipe->rd_wait, EPOLLIN | EPOLLRDNORM);
+       done = true;
+ out:
+       spin_unlock_irq(&pipe->rd_wait.lock);
+       if (done)
+               kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
+       return done;
+ lost:
+       buf = &pipe->bufs[(head - 1) & mask];
+       buf->flags |= PIPE_BUF_FLAG_LOSS;
+       goto out;
+ }
+ /*
+  * Apply filter rules to a notification.
+  */
+ static bool filter_watch_notification(const struct watch_filter *wf,
+                                     const struct watch_notification *n)
+ {
+       const struct watch_type_filter *wt;
+       unsigned int st_bits = sizeof(wt->subtype_filter[0]) * 8;
+       unsigned int st_index = n->subtype / st_bits;
+       unsigned int st_bit = 1U << (n->subtype % st_bits);
+       int i;
+       if (!test_bit(n->type, wf->type_filter))
+               return false;
+       for (i = 0; i < wf->nr_filters; i++) {
+               wt = &wf->filters[i];
+               if (n->type == wt->type &&
+                   (wt->subtype_filter[st_index] & st_bit) &&
+                   (n->info & wt->info_mask) == wt->info_filter)
+                       return true;
+       }
+       return false; /* If there is a filter, the default is to reject. */
+ }
+ /**
+  * __post_watch_notification - Post an event notification
+  * @wlist: The watch list to post the event to.
+  * @n: The notification record to post.
+  * @cred: The creds of the process that triggered the notification.
+  * @id: The ID to match on the watch.
+  *
+  * Post a notification of an event into a set of watch queues and let the users
+  * know.
+  *
+  * The size of the notification should be set in n->info & WATCH_INFO_LENGTH and
+  * should be in units of sizeof(*n).
+  */
+ void __post_watch_notification(struct watch_list *wlist,
+                              struct watch_notification *n,
+                              const struct cred *cred,
+                              u64 id)
+ {
+       const struct watch_filter *wf;
+       struct watch_queue *wqueue;
+       struct watch *watch;
+       if (((n->info & WATCH_INFO_LENGTH) >> WATCH_INFO_LENGTH__SHIFT) == 0) {
+               WARN_ON(1);
+               return;
+       }
+       rcu_read_lock();
+       hlist_for_each_entry_rcu(watch, &wlist->watchers, list_node) {
+               if (watch->id != id)
+                       continue;
+               n->info &= ~WATCH_INFO_ID;
+               n->info |= watch->info_id;
+               wqueue = rcu_dereference(watch->queue);
+               wf = rcu_dereference(wqueue->filter);
+               if (wf && !filter_watch_notification(wf, n))
+                       continue;
+               if (security_post_notification(watch->cred, cred, n) < 0)
+                       continue;
+               post_one_notification(wqueue, n);
+       }
+       rcu_read_unlock();
+ }
+ EXPORT_SYMBOL(__post_watch_notification);
+ /*
+  * Allocate sufficient pages to preallocation for the requested number of
+  * notifications.
+  */
+ long watch_queue_set_size(struct pipe_inode_info *pipe, unsigned int nr_notes)
+ {
+       struct watch_queue *wqueue = pipe->watch_queue;
+       struct page **pages;
+       unsigned long *bitmap;
+       unsigned long user_bufs;
+       unsigned int bmsize;
+       int ret, i, nr_pages;
+       if (!wqueue)
+               return -ENODEV;
+       if (wqueue->notes)
+               return -EBUSY;
+       if (nr_notes < 1 ||
+           nr_notes > 512) /* TODO: choose a better hard limit */
+               return -EINVAL;
+       nr_pages = (nr_notes + WATCH_QUEUE_NOTES_PER_PAGE - 1);
+       nr_pages /= WATCH_QUEUE_NOTES_PER_PAGE;
+       user_bufs = account_pipe_buffers(pipe->user, pipe->nr_accounted, nr_pages);
+       if (nr_pages > pipe->max_usage &&
+           (too_many_pipe_buffers_hard(user_bufs) ||
+            too_many_pipe_buffers_soft(user_bufs)) &&
+           pipe_is_unprivileged_user()) {
+               ret = -EPERM;
+               goto error;
+       }
+       ret = pipe_resize_ring(pipe, nr_notes);
+       if (ret < 0)
+               goto error;
+       pages = kcalloc(sizeof(struct page *), nr_pages, GFP_KERNEL);
+       if (!pages)
+               goto error;
+       for (i = 0; i < nr_pages; i++) {
+               pages[i] = alloc_page(GFP_KERNEL);
+               if (!pages[i])
+                       goto error_p;
+               pages[i]->index = i * WATCH_QUEUE_NOTES_PER_PAGE;
+       }
+       bmsize = (nr_notes + BITS_PER_LONG - 1) / BITS_PER_LONG;
+       bmsize *= sizeof(unsigned long);
+       bitmap = kmalloc(bmsize, GFP_KERNEL);
+       if (!bitmap)
+               goto error_p;
+       memset(bitmap, 0xff, bmsize);
+       wqueue->notes = pages;
+       wqueue->notes_bitmap = bitmap;
+       wqueue->nr_pages = nr_pages;
+       wqueue->nr_notes = nr_pages * WATCH_QUEUE_NOTES_PER_PAGE;
+       return 0;
+ error_p:
+       for (i = 0; i < nr_pages; i++)
+               __free_page(pages[i]);
+       kfree(pages);
+ error:
+       (void) account_pipe_buffers(pipe->user, nr_pages, pipe->nr_accounted);
+       return ret;
+ }
+ /*
+  * Set the filter on a watch queue.
+  */
+ long watch_queue_set_filter(struct pipe_inode_info *pipe,
+                           struct watch_notification_filter __user *_filter)
+ {
+       struct watch_notification_type_filter *tf;
+       struct watch_notification_filter filter;
+       struct watch_type_filter *q;
+       struct watch_filter *wfilter;
+       struct watch_queue *wqueue = pipe->watch_queue;
+       int ret, nr_filter = 0, i;
+       if (!wqueue)
+               return -ENODEV;
+       if (!_filter) {
+               /* Remove the old filter */
+               wfilter = NULL;
+               goto set;
+       }
+       /* Grab the user's filter specification */
+       if (copy_from_user(&filter, _filter, sizeof(filter)) != 0)
+               return -EFAULT;
+       if (filter.nr_filters == 0 ||
+           filter.nr_filters > 16 ||
+           filter.__reserved != 0)
+               return -EINVAL;
+       tf = memdup_user(_filter->filters, filter.nr_filters * sizeof(*tf));
+       if (IS_ERR(tf))
+               return PTR_ERR(tf);
+       ret = -EINVAL;
+       for (i = 0; i < filter.nr_filters; i++) {
+               if ((tf[i].info_filter & ~tf[i].info_mask) ||
+                   tf[i].info_mask & WATCH_INFO_LENGTH)
+                       goto err_filter;
+               /* Ignore any unknown types */
+               if (tf[i].type >= sizeof(wfilter->type_filter) * 8)
+                       continue;
+               nr_filter++;
+       }
+       /* Now we need to build the internal filter from only the relevant
+        * user-specified filters.
+        */
+       ret = -ENOMEM;
+       wfilter = kzalloc(struct_size(wfilter, filters, nr_filter), GFP_KERNEL);
+       if (!wfilter)
+               goto err_filter;
+       wfilter->nr_filters = nr_filter;
+       q = wfilter->filters;
+       for (i = 0; i < filter.nr_filters; i++) {
+               if (tf[i].type >= sizeof(wfilter->type_filter) * BITS_PER_LONG)
+                       continue;
+               q->type                 = tf[i].type;
+               q->info_filter          = tf[i].info_filter;
+               q->info_mask            = tf[i].info_mask;
+               q->subtype_filter[0]    = tf[i].subtype_filter[0];
+               __set_bit(q->type, wfilter->type_filter);
+               q++;
+       }
+       kfree(tf);
+ set:
+       pipe_lock(pipe);
+       wfilter = rcu_replace_pointer(wqueue->filter, wfilter,
+                                     lockdep_is_held(&pipe->mutex));
+       pipe_unlock(pipe);
+       if (wfilter)
+               kfree_rcu(wfilter, rcu);
+       return 0;
+ err_filter:
+       kfree(tf);
+       return ret;
+ }
+ static void __put_watch_queue(struct kref *kref)
+ {
+       struct watch_queue *wqueue =
+               container_of(kref, struct watch_queue, usage);
+       struct watch_filter *wfilter;
+       int i;
+       for (i = 0; i < wqueue->nr_pages; i++)
+               __free_page(wqueue->notes[i]);
+       wfilter = rcu_access_pointer(wqueue->filter);
+       if (wfilter)
+               kfree_rcu(wfilter, rcu);
+       kfree_rcu(wqueue, rcu);
+ }
+ /**
+  * put_watch_queue - Dispose of a ref on a watchqueue.
+  * @wqueue: The watch queue to unref.
+  */
+ void put_watch_queue(struct watch_queue *wqueue)
+ {
+       kref_put(&wqueue->usage, __put_watch_queue);
+ }
+ EXPORT_SYMBOL(put_watch_queue);
+ static void free_watch(struct rcu_head *rcu)
+ {
+       struct watch *watch = container_of(rcu, struct watch, rcu);
+       put_watch_queue(rcu_access_pointer(watch->queue));
+       put_cred(watch->cred);
+ }
+ static void __put_watch(struct kref *kref)
+ {
+       struct watch *watch = container_of(kref, struct watch, usage);
+       call_rcu(&watch->rcu, free_watch);
+ }
+ /*
+  * Discard a watch.
+  */
+ static void put_watch(struct watch *watch)
+ {
+       kref_put(&watch->usage, __put_watch);
+ }
+ /**
+  * init_watch_queue - Initialise a watch
+  * @watch: The watch to initialise.
+  * @wqueue: The queue to assign.
+  *
+  * Initialise a watch and set the watch queue.
+  */
+ void init_watch(struct watch *watch, struct watch_queue *wqueue)
+ {
+       kref_init(&watch->usage);
+       INIT_HLIST_NODE(&watch->list_node);
+       INIT_HLIST_NODE(&watch->queue_node);
+       rcu_assign_pointer(watch->queue, wqueue);
+ }
+ /**
+  * add_watch_to_object - Add a watch on an object to a watch list
+  * @watch: The watch to add
+  * @wlist: The watch list to add to
+  *
+  * @watch->queue must have been set to point to the queue to post notifications
+  * to and the watch list of the object to be watched.  @watch->cred must also
+  * have been set to the appropriate credentials and a ref taken on them.
+  *
+  * The caller must pin the queue and the list both and must hold the list
+  * locked against racing watch additions/removals.
+  */
+ int add_watch_to_object(struct watch *watch, struct watch_list *wlist)
+ {
+       struct watch_queue *wqueue = rcu_access_pointer(watch->queue);
+       struct watch *w;
+       hlist_for_each_entry(w, &wlist->watchers, list_node) {
+               struct watch_queue *wq = rcu_access_pointer(w->queue);
+               if (wqueue == wq && watch->id == w->id)
+                       return -EBUSY;
+       }
+       watch->cred = get_current_cred();
+       rcu_assign_pointer(watch->watch_list, wlist);
+       spin_lock_bh(&wqueue->lock);
+       kref_get(&wqueue->usage);
+       kref_get(&watch->usage);
+       hlist_add_head(&watch->queue_node, &wqueue->watches);
+       spin_unlock_bh(&wqueue->lock);
+       hlist_add_head(&watch->list_node, &wlist->watchers);
+       return 0;
+ }
+ EXPORT_SYMBOL(add_watch_to_object);
+ /**
+  * remove_watch_from_object - Remove a watch or all watches from an object.
+  * @wlist: The watch list to remove from
+  * @wq: The watch queue of interest (ignored if @all is true)
+  * @id: The ID of the watch to remove (ignored if @all is true)
+  * @all: True to remove all objects
+  *
+  * Remove a specific watch or all watches from an object.  A notification is
+  * sent to the watcher to tell them that this happened.
+  */
+ int remove_watch_from_object(struct watch_list *wlist, struct watch_queue *wq,
+                            u64 id, bool all)
+ {
+       struct watch_notification_removal n;
+       struct watch_queue *wqueue;
+       struct watch *watch;
+       int ret = -EBADSLT;
+       rcu_read_lock();
+ again:
+       spin_lock(&wlist->lock);
+       hlist_for_each_entry(watch, &wlist->watchers, list_node) {
+               if (all ||
+                   (watch->id == id && rcu_access_pointer(watch->queue) == wq))
+                       goto found;
+       }
+       spin_unlock(&wlist->lock);
+       goto out;
+ found:
+       ret = 0;
+       hlist_del_init_rcu(&watch->list_node);
+       rcu_assign_pointer(watch->watch_list, NULL);
+       spin_unlock(&wlist->lock);
+       /* We now own the reference on watch that used to belong to wlist. */
+       n.watch.type = WATCH_TYPE_META;
+       n.watch.subtype = WATCH_META_REMOVAL_NOTIFICATION;
+       n.watch.info = watch->info_id | watch_sizeof(n.watch);
+       n.id = id;
+       if (id != 0)
+               n.watch.info = watch->info_id | watch_sizeof(n);
+       wqueue = rcu_dereference(watch->queue);
+       /* We don't need the watch list lock for the next bit as RCU is
+        * protecting *wqueue from deallocation.
+        */
+       if (wqueue) {
+               post_one_notification(wqueue, &n.watch);
+               spin_lock_bh(&wqueue->lock);
+               if (!hlist_unhashed(&watch->queue_node)) {
+                       hlist_del_init_rcu(&watch->queue_node);
+                       put_watch(watch);
+               }
+               spin_unlock_bh(&wqueue->lock);
+       }
+       if (wlist->release_watch) {
+               void (*release_watch)(struct watch *);
+               release_watch = wlist->release_watch;
+               rcu_read_unlock();
+               (*release_watch)(watch);
+               rcu_read_lock();
+       }
+       put_watch(watch);
+       if (all && !hlist_empty(&wlist->watchers))
+               goto again;
+ out:
+       rcu_read_unlock();
+       return ret;
+ }
+ EXPORT_SYMBOL(remove_watch_from_object);
+ /*
+  * Remove all the watches that are contributory to a queue.  This has the
+  * potential to race with removal of the watches by the destruction of the
+  * objects being watched or with the distribution of notifications.
+  */
+ void watch_queue_clear(struct watch_queue *wqueue)
+ {
+       struct watch_list *wlist;
+       struct watch *watch;
+       bool release;
+       rcu_read_lock();
+       spin_lock_bh(&wqueue->lock);
+       /* Prevent new additions and prevent notifications from happening */
+       wqueue->defunct = true;
+       while (!hlist_empty(&wqueue->watches)) {
+               watch = hlist_entry(wqueue->watches.first, struct watch, queue_node);
+               hlist_del_init_rcu(&watch->queue_node);
+               /* We now own a ref on the watch. */
+               spin_unlock_bh(&wqueue->lock);
+               /* We can't do the next bit under the queue lock as we need to
+                * get the list lock - which would cause a deadlock if someone
+                * was removing from the opposite direction at the same time or
+                * posting a notification.
+                */
+               wlist = rcu_dereference(watch->watch_list);
+               if (wlist) {
+                       void (*release_watch)(struct watch *);
+                       spin_lock(&wlist->lock);
+                       release = !hlist_unhashed(&watch->list_node);
+                       if (release) {
+                               hlist_del_init_rcu(&watch->list_node);
+                               rcu_assign_pointer(watch->watch_list, NULL);
+                               /* We now own a second ref on the watch. */
+                       }
+                       release_watch = wlist->release_watch;
+                       spin_unlock(&wlist->lock);
+                       if (release) {
+                               if (release_watch) {
+                                       rcu_read_unlock();
+                                       /* This might need to call dput(), so
+                                        * we have to drop all the locks.
+                                        */
+                                       (*release_watch)(watch);
+                                       rcu_read_lock();
+                               }
+                               put_watch(watch);
+                       }
+               }
+               put_watch(watch);
+               spin_lock_bh(&wqueue->lock);
+       }
+       spin_unlock_bh(&wqueue->lock);
+       rcu_read_unlock();
+ }
+ /**
+  * get_watch_queue - Get a watch queue from its file descriptor.
+  * @fd: The fd to query.
+  */
+ struct watch_queue *get_watch_queue(int fd)
+ {
+       struct pipe_inode_info *pipe;
+       struct watch_queue *wqueue = ERR_PTR(-EINVAL);
+       struct fd f;
+       f = fdget(fd);
+       if (f.file) {
+               pipe = get_pipe_info(f.file, false);
+               if (pipe && pipe->watch_queue) {
+                       wqueue = pipe->watch_queue;
+                       kref_get(&wqueue->usage);
+               }
+               fdput(f);
+       }
+       return wqueue;
+ }
+ EXPORT_SYMBOL(get_watch_queue);
+ /*
+  * Initialise a watch queue
+  */
+ int watch_queue_init(struct pipe_inode_info *pipe)
+ {
+       struct watch_queue *wqueue;
+       wqueue = kzalloc(sizeof(*wqueue), GFP_KERNEL);
+       if (!wqueue)
+               return -ENOMEM;
+       wqueue->pipe = pipe;
+       kref_init(&wqueue->usage);
+       spin_lock_init(&wqueue->lock);
+       INIT_HLIST_HEAD(&wqueue->watches);
+       pipe->watch_queue = wqueue;
+       return 0;
+ }
diff --combined samples/Kconfig
@@@ -6,10 -6,6 +6,10 @@@ menuconfig SAMPLE
  
  if SAMPLES
  
 +config SAMPLE_AUXDISPLAY
 +      bool "auxdisplay sample"
 +      depends on CC_CAN_LINK
 +
  config SAMPLE_TRACE_EVENTS
        tristate "Build trace_events examples -- loadable modules only"
        depends on EVENT_TRACING && m
@@@ -122,29 -118,19 +122,29 @@@ config SAMPLE_CONNECTO
  
  config SAMPLE_HIDRAW
        bool "hidraw sample"
 -      depends on HEADERS_INSTALL
 +      depends on CC_CAN_LINK && HEADERS_INSTALL
  
  config SAMPLE_PIDFD
        bool "pidfd sample"
 -      depends on HEADERS_INSTALL
 +      depends on CC_CAN_LINK && HEADERS_INSTALL
  
  config SAMPLE_SECCOMP
        bool "Build seccomp sample code"
 -      depends on SECCOMP_FILTER && HEADERS_INSTALL
 +      depends on SECCOMP_FILTER && CC_CAN_LINK && HEADERS_INSTALL
        help
          Build samples of seccomp filters using various methods of
          BPF filter construction.
  
 +config SAMPLE_TIMER
 +      bool "Timer sample"
 +      depends on CC_CAN_LINK && HEADERS_INSTALL
 +
 +config SAMPLE_UHID
 +      bool "UHID sample"
 +      depends on CC_CAN_LINK && HEADERS_INSTALL
 +      help
 +        Build UHID sample program.
 +
  config SAMPLE_VFIO_MDEV_MTTY
        tristate "Build VFIO mtty example mediated device sample code -- loadable modules only"
        depends on VFIO_MDEV_DEVICE && m
@@@ -185,14 -171,14 +185,14 @@@ config SAMPLE_VFIO_MDEV_MBOCH
  
  config SAMPLE_ANDROID_BINDERFS
        bool "Build Android binderfs example"
 -      depends on CONFIG_ANDROID_BINDERFS
 +      depends on ANDROID_BINDERFS
        help
          Builds a sample program to illustrate the use of the Android binderfs
          filesystem.
  
  config SAMPLE_VFS
        bool "Build example programs that use new VFS system calls"
 -      depends on HEADERS_INSTALL
 +      depends on CC_CAN_LINK && HEADERS_INSTALL
        help
          Build example userspace programs that use new VFS system calls such
          as mount API and statx().  Note that this is restricted to the x86
  config SAMPLE_INTEL_MEI
        bool "Build example program working with intel mei driver"
        depends on INTEL_MEI
 +      depends on CC_CAN_LINK && HEADERS_INSTALL
        help
          Build a sample program to work with mei device.
  
 +config SAMPLE_WATCHDOG
 +      bool "watchdog sample"
 +      depends on CC_CAN_LINK
 +
+ config SAMPLE_WATCH_QUEUE
+       bool "Build example /dev/watch_queue notification consumer"
+       depends on HEADERS_INSTALL
+       help
+         Build example userspace program to use the new mount_notify(),
+         sb_notify() syscalls and the KEYCTL_WATCH_KEY keyctl() function.
  endif # SAMPLES
diff --combined samples/Makefile
@@@ -1,7 -1,7 +1,7 @@@
  # SPDX-License-Identifier: GPL-2.0
  # Makefile for Linux samples code
 -OBJECT_FILES_NON_STANDARD := y
  
 +subdir-$(CONFIG_SAMPLE_AUXDISPLAY)    += auxdisplay
  obj-$(CONFIG_SAMPLE_ANDROID_BINDERFS) += binderfs/
  obj-$(CONFIG_SAMPLE_CONFIGFS)         += configfs/
  obj-$(CONFIG_SAMPLE_CONNECTOR)                += connector/
@@@ -16,14 -16,12 +16,15 @@@ subdir-$(CONFIG_SAMPLE_PIDFD)              += pidf
  obj-$(CONFIG_SAMPLE_QMI_CLIENT)               += qmi/
  obj-$(CONFIG_SAMPLE_RPMSG_CLIENT)     += rpmsg/
  subdir-$(CONFIG_SAMPLE_SECCOMP)               += seccomp
 +subdir-$(CONFIG_SAMPLE_TIMER)         += timers
  obj-$(CONFIG_SAMPLE_TRACE_EVENTS)     += trace_events/
  obj-$(CONFIG_SAMPLE_TRACE_PRINTK)     += trace_printk/
  obj-$(CONFIG_SAMPLE_FTRACE_DIRECT)    += ftrace/
  obj-$(CONFIG_SAMPLE_TRACE_ARRAY)      += ftrace/
 +subdir-$(CONFIG_SAMPLE_UHID)          += uhid
  obj-$(CONFIG_VIDEO_PCI_SKELETON)      += v4l/
  obj-y                                 += vfio-mdev/
  subdir-$(CONFIG_SAMPLE_VFS)           += vfs
  obj-$(CONFIG_SAMPLE_INTEL_MEI)                += mei/
 +subdir-$(CONFIG_SAMPLE_WATCHDOG)      += watchdog
+ subdir-$(CONFIG_SAMPLE_WATCH_QUEUE)   += watch_queue
diff --combined security/keys/Kconfig
@@@ -60,7 -60,9 +60,7 @@@ config BIG_KEY
        bool "Large payload keys"
        depends on KEYS
        depends on TMPFS
 -      select CRYPTO
 -      select CRYPTO_AES
 -      select CRYPTO_GCM
 +      depends on CRYPTO_LIB_CHACHA20POLY1305 = y
        help
          This option provides support for holding large keys within the kernel
          (for example Kerberos ticket caches).  The data may be stored out to
@@@ -114,3 -116,12 +114,12 @@@ config KEY_DH_OPERATION
         in the kernel.
  
         If you are unsure as to whether this is required, answer N.
+ config KEY_NOTIFICATIONS
+       bool "Provide key/keyring change notifications"
+       depends on KEYS && WATCH_QUEUE
+       help
+         This option provides support for getting change notifications on keys
+         and keyrings on which the caller has View permission.  This makes use
+         of the /dev/watch_queue misc device to handle the notification
+         buffer and provides KEYCTL_WATCH_KEY to enable/disable watches.
diff --combined security/keys/internal.h
@@@ -15,6 -15,7 +15,7 @@@
  #include <linux/task_work.h>
  #include <linux/keyctl.h>
  #include <linux/refcount.h>
+ #include <linux/watch_queue.h>
  #include <linux/compat.h>
  #include <linux/mm.h>
  #include <linux/vmalloc.h>
@@@ -99,7 -100,8 +100,8 @@@ extern int __key_link_begin(struct key 
                            const struct keyring_index_key *index_key,
                            struct assoc_array_edit **_edit);
  extern int __key_link_check_live_key(struct key *keyring, struct key *key);
- extern void __key_link(struct key *key, struct assoc_array_edit **_edit);
+ extern void __key_link(struct key *keyring, struct key *key,
+                      struct assoc_array_edit **_edit);
  extern void __key_link_end(struct key *keyring,
                           const struct keyring_index_key *index_key,
                           struct assoc_array_edit *edit);
@@@ -165,7 -167,6 +167,6 @@@ extern bool lookup_user_key_possessed(c
                                      const struct key_match_data *match_data);
  #define KEY_LOOKUP_CREATE     0x01
  #define KEY_LOOKUP_PARTIAL    0x02
- #define KEY_LOOKUP_FOR_UNLINK 0x04
  
  extern long join_session_keyring(const char *name);
  extern void key_change_session_keyring(struct callback_head *twork);
@@@ -181,14 -182,32 +182,32 @@@ extern void key_gc_keytype(struct key_t
  
  extern int key_task_permission(const key_ref_t key_ref,
                               const struct cred *cred,
-                              key_perm_t perm);
+                              enum key_need_perm need_perm);
+ static inline void notify_key(struct key *key,
+                             enum key_notification_subtype subtype, u32 aux)
+ {
+ #ifdef CONFIG_KEY_NOTIFICATIONS
+       struct key_notification n = {
+               .watch.type     = WATCH_TYPE_KEY_NOTIFY,
+               .watch.subtype  = subtype,
+               .watch.info     = watch_sizeof(n),
+               .key_id         = key_serial(key),
+               .aux            = aux,
+       };
+       post_watch_notification(key->watchers, &n.watch, current_cred(),
+                               n.key_id);
+ #endif
+ }
  
  /*
   * Check to see whether permission is granted to use a key in the desired way.
   */
- static inline int key_permission(const key_ref_t key_ref, unsigned perm)
+ static inline int key_permission(const key_ref_t key_ref,
+                                enum key_need_perm need_perm)
  {
-       return key_task_permission(key_ref, current_cred(), perm);
+       return key_task_permission(key_ref, current_cred(), need_perm);
  }
  
  extern struct key_type key_type_request_key_auth;
@@@ -333,6 -352,15 +352,15 @@@ static inline long keyctl_pkey_e_d_s(in
  
  extern long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen);
  
+ #ifdef CONFIG_KEY_NOTIFICATIONS
+ extern long keyctl_watch_key(key_serial_t, int, int);
+ #else
+ static inline long keyctl_watch_key(key_serial_t key_id, int watch_fd, int watch_id)
+ {
+       return -EOPNOTSUPP;
+ }
+ #endif
  /*
   * Debugging key validation
   */
@@@ -350,4 -378,15 +378,4 @@@ static inline void key_check(const stru
  #define key_check(key) do {} while(0)
  
  #endif
 -
 -/*
 - * Helper function to clear and free a kvmalloc'ed memory object.
 - */
 -static inline void __kvzfree(const void *addr, size_t len)
 -{
 -      if (addr) {
 -              memset((void *)addr, 0, len);
 -              kvfree(addr);
 -      }
 -}
  #endif /* _INTERNAL_H */
diff --combined security/keys/keyctl.c
@@@ -37,7 -37,9 +37,9 @@@ static const unsigned char keyrings_cap
               KEYCTL_CAPS0_MOVE
               ),
        [1] = (KEYCTL_CAPS1_NS_KEYRING_NAME |
-              KEYCTL_CAPS1_NS_KEY_TAG),
+              KEYCTL_CAPS1_NS_KEY_TAG |
+              (IS_ENABLED(CONFIG_KEY_NOTIFICATIONS)    ? KEYCTL_CAPS1_NOTIFICATIONS : 0)
+              ),
  };
  
  static int key_get_type_from_user(char *type,
@@@ -142,7 -144,10 +144,7 @@@ SYSCALL_DEFINE5(add_key, const char __u
  
        key_ref_put(keyring_ref);
   error3:
 -      if (payload) {
 -              memzero_explicit(payload, plen);
 -              kvfree(payload);
 -      }
 +      kvfree_sensitive(payload, plen);
   error2:
        kfree(description);
   error:
@@@ -357,7 -362,7 +359,7 @@@ long keyctl_update_key(key_serial_t id
  
        key_ref_put(key_ref);
  error2:
 -      __kvzfree(payload, plen);
 +      kvfree_sensitive(payload, plen);
  error:
        return ret;
  }
@@@ -429,7 -434,7 +431,7 @@@ long keyctl_invalidate_key(key_serial_
  
                /* Root is permitted to invalidate certain special keys */
                if (capable(CAP_SYS_ADMIN)) {
-                       key_ref = lookup_user_key(id, 0, 0);
+                       key_ref = lookup_user_key(id, 0, KEY_SYSADMIN_OVERRIDE);
                        if (IS_ERR(key_ref))
                                goto error;
                        if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
@@@ -474,7 -479,8 +476,8 @@@ long keyctl_keyring_clear(key_serial_t 
  
                /* Root is permitted to invalidate certain special keyrings */
                if (capable(CAP_SYS_ADMIN)) {
-                       keyring_ref = lookup_user_key(ringid, 0, 0);
+                       keyring_ref = lookup_user_key(ringid, 0,
+                                                     KEY_SYSADMIN_OVERRIDE);
                        if (IS_ERR(keyring_ref))
                                goto error;
                        if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
@@@ -558,7 -564,7 +561,7 @@@ long keyctl_keyring_unlink(key_serial_
                goto error;
        }
  
-       key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
+       key_ref = lookup_user_key(id, KEY_LOOKUP_PARTIAL, KEY_NEED_UNLINK);
        if (IS_ERR(key_ref)) {
                ret = PTR_ERR(key_ref);
                goto error2;
@@@ -658,7 -664,7 +661,7 @@@ long keyctl_describe_key(key_serial_t k
                                key_put(instkey);
                                key_ref = lookup_user_key(keyid,
                                                          KEY_LOOKUP_PARTIAL,
-                                                         0);
+                                                         KEY_AUTHTOKEN_OVERRIDE);
                                if (!IS_ERR(key_ref))
                                        goto okay;
                        }
@@@ -828,7 -834,7 +831,7 @@@ long keyctl_read_key(key_serial_t keyid
        size_t key_data_len;
  
        /* find the key first */
-       key_ref = lookup_user_key(keyid, 0, 0);
+       key_ref = lookup_user_key(keyid, 0, KEY_DEFER_PERM_CHECK);
        if (IS_ERR(key_ref)) {
                ret = -ENOKEY;
                goto out;
@@@ -875,7 -881,7 +878,7 @@@ can_read_key
         *
         * Allocating a temporary buffer to hold the keys before
         * transferring them to user buffer to avoid potential
 -       * deadlock involving page fault and mmap_sem.
 +       * deadlock involving page fault and mmap_lock.
         *
         * key_data_len = (buflen <= PAGE_SIZE)
         *              ? buflen : actual length of key data
                 */
                if (ret > key_data_len) {
                        if (unlikely(key_data))
 -                              __kvzfree(key_data, key_data_len);
 +                              kvfree_sensitive(key_data, key_data_len);
                        key_data_len = ret;
                        continue;       /* Allocate buffer */
                }
                        ret = -EFAULT;
                break;
        }
 -      __kvzfree(key_data, key_data_len);
 +      kvfree_sensitive(key_data, key_data_len);
  
  key_put_out:
        key_put(key);
@@@ -1036,6 -1042,7 +1039,7 @@@ long keyctl_chown_key(key_serial_t id, 
        if (group != (gid_t) -1)
                key->gid = gid;
  
+       notify_key(key, NOTIFY_KEY_SETATTR, 0);
        ret = 0;
  
  error_put:
@@@ -1086,6 -1093,7 +1090,7 @@@ long keyctl_setperm_key(key_serial_t id
        /* if we're not the sysadmin, we can only change a key that we own */
        if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
                key->perm = perm;
+               notify_key(key, NOTIFY_KEY_SETATTR, 0);
                ret = 0;
        }
  
@@@ -1222,7 -1230,10 +1227,7 @@@ long keyctl_instantiate_key_common(key_
                keyctl_change_reqkey_auth(NULL);
  
  error2:
 -      if (payload) {
 -              memzero_explicit(payload, plen);
 -              kvfree(payload);
 -      }
 +      kvfree_sensitive(payload, plen);
  error:
        return ret;
  }
@@@ -1461,7 -1472,7 +1466,7 @@@ long keyctl_set_timeout(key_serial_t id
                                key_put(instkey);
                                key_ref = lookup_user_key(id,
                                                          KEY_LOOKUP_PARTIAL,
-                                                         0);
+                                                         KEY_AUTHTOKEN_OVERRIDE);
                                if (!IS_ERR(key_ref))
                                        goto okay;
                        }
  okay:
        key = key_ref_to_ptr(key_ref);
        ret = 0;
-       if (test_bit(KEY_FLAG_KEEP, &key->flags))
+       if (test_bit(KEY_FLAG_KEEP, &key->flags)) {
                ret = -EPERM;
-       else
+       } else {
                key_set_timeout(key, timeout);
+               notify_key(key, NOTIFY_KEY_SETATTR, 0);
+       }
        key_put(key);
  
  error:
@@@ -1567,7 -1580,8 +1574,8 @@@ long keyctl_get_security(key_serial_t k
                        return PTR_ERR(instkey);
                key_put(instkey);
  
-               key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
+               key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL,
+                                         KEY_AUTHTOKEN_OVERRIDE);
                if (IS_ERR(key_ref))
                        return PTR_ERR(key_ref);
        }
@@@ -1751,6 -1765,90 +1759,90 @@@ error
        return ret;
  }
  
+ #ifdef CONFIG_KEY_NOTIFICATIONS
+ /*
+  * Watch for changes to a key.
+  *
+  * The caller must have View permission to watch a key or keyring.
+  */
+ long keyctl_watch_key(key_serial_t id, int watch_queue_fd, int watch_id)
+ {
+       struct watch_queue *wqueue;
+       struct watch_list *wlist = NULL;
+       struct watch *watch = NULL;
+       struct key *key;
+       key_ref_t key_ref;
+       long ret;
+       if (watch_id < -1 || watch_id > 0xff)
+               return -EINVAL;
+       key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_VIEW);
+       if (IS_ERR(key_ref))
+               return PTR_ERR(key_ref);
+       key = key_ref_to_ptr(key_ref);
+       wqueue = get_watch_queue(watch_queue_fd);
+       if (IS_ERR(wqueue)) {
+               ret = PTR_ERR(wqueue);
+               goto err_key;
+       }
+       if (watch_id >= 0) {
+               ret = -ENOMEM;
+               if (!key->watchers) {
+                       wlist = kzalloc(sizeof(*wlist), GFP_KERNEL);
+                       if (!wlist)
+                               goto err_wqueue;
+                       init_watch_list(wlist, NULL);
+               }
+               watch = kzalloc(sizeof(*watch), GFP_KERNEL);
+               if (!watch)
+                       goto err_wlist;
+               init_watch(watch, wqueue);
+               watch->id       = key->serial;
+               watch->info_id  = (u32)watch_id << WATCH_INFO_ID__SHIFT;
+               ret = security_watch_key(key);
+               if (ret < 0)
+                       goto err_watch;
+               down_write(&key->sem);
+               if (!key->watchers) {
+                       key->watchers = wlist;
+                       wlist = NULL;
+               }
+               ret = add_watch_to_object(watch, key->watchers);
+               up_write(&key->sem);
+               if (ret == 0)
+                       watch = NULL;
+       } else {
+               ret = -EBADSLT;
+               if (key->watchers) {
+                       down_write(&key->sem);
+                       ret = remove_watch_from_object(key->watchers,
+                                                      wqueue, key_serial(key),
+                                                      false);
+                       up_write(&key->sem);
+               }
+       }
+ err_watch:
+       kfree(watch);
+ err_wlist:
+       kfree(wlist);
+ err_wqueue:
+       put_watch_queue(wqueue);
+ err_key:
+       key_put(key);
+       return ret;
+ }
+ #endif /* CONFIG_KEY_NOTIFICATIONS */
  /*
   * Get keyrings subsystem capabilities.
   */
@@@ -1920,6 -2018,9 +2012,9 @@@ SYSCALL_DEFINE5(keyctl, int, option, un
        case KEYCTL_CAPABILITIES:
                return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
  
+       case KEYCTL_WATCH_KEY:
+               return keyctl_watch_key((key_serial_t)arg2, (int)arg3, (int)arg4);
        default:
                return -EOPNOTSUPP;
        }
diff --combined security/security.c
@@@ -823,14 -823,9 +823,14 @@@ int security_vm_enough_memory_mm(struc
        return __vm_enough_memory(mm, pages, cap_sys_admin);
  }
  
 -int security_bprm_set_creds(struct linux_binprm *bprm)
 +int security_bprm_creds_for_exec(struct linux_binprm *bprm)
  {
 -      return call_int_hook(bprm_set_creds, 0, bprm);
 +      return call_int_hook(bprm_creds_for_exec, 0, bprm);
 +}
 +
 +int security_bprm_creds_from_file(struct linux_binprm *bprm, struct file *file)
 +{
 +      return call_int_hook(bprm_creds_from_file, 0, bprm, file);
  }
  
  int security_bprm_check(struct linux_binprm *bprm)
@@@ -1464,7 -1459,6 +1464,7 @@@ int security_file_ioctl(struct file *fi
  {
        return call_int_hook(file_ioctl, 0, file, cmd, arg);
  }
 +EXPORT_SYMBOL_GPL(security_file_ioctl);
  
  static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
  {
@@@ -1518,12 -1512,7 +1518,12 @@@ int security_mmap_addr(unsigned long ad
  int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
                            unsigned long prot)
  {
 -      return call_int_hook(file_mprotect, 0, vma, reqprot, prot);
 +      int ret;
 +
 +      ret = call_int_hook(file_mprotect, 0, vma, reqprot, prot);
 +      if (ret)
 +              return ret;
 +      return ima_file_mprotect(vma, prot);
  }
  
  int security_file_lock(struct file *file, unsigned int cmd)
@@@ -1976,20 -1965,8 +1976,20 @@@ EXPORT_SYMBOL(security_ismaclabel)
  
  int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
  {
 -      return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid, secdata,
 -                              seclen);
 +      struct security_hook_list *hp;
 +      int rc;
 +
 +      /*
 +       * Currently, only one LSM can implement secid_to_secctx (i.e this
 +       * LSM hook is not "stackable").
 +       */
 +      hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
 +              rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
 +              if (rc != LSM_RET_DEFAULT(secid_to_secctx))
 +                      return rc;
 +      }
 +
 +      return LSM_RET_DEFAULT(secid_to_secctx);
  }
  EXPORT_SYMBOL(security_secid_to_secctx);
  
@@@ -2030,6 -2007,22 +2030,22 @@@ int security_inode_getsecctx(struct ino
  }
  EXPORT_SYMBOL(security_inode_getsecctx);
  
+ #ifdef CONFIG_WATCH_QUEUE
+ int security_post_notification(const struct cred *w_cred,
+                              const struct cred *cred,
+                              struct watch_notification *n)
+ {
+       return call_int_hook(post_notification, 0, w_cred, cred, n);
+ }
+ #endif /* CONFIG_WATCH_QUEUE */
+ #ifdef CONFIG_KEY_NOTIFICATIONS
+ int security_watch_key(struct key *key)
+ {
+       return call_int_hook(watch_key, 0, key);
+ }
+ #endif
  #ifdef CONFIG_SECURITY_NETWORK
  
  int security_unix_stream_connect(struct sock *sock, struct sock *other, struct sock *newsk)
@@@ -2405,10 -2398,10 +2421,10 @@@ void security_key_free(struct key *key
        call_void_hook(key_free, key);
  }
  
- int security_key_permission(key_ref_t key_ref,
-                           const struct cred *cred, unsigned perm)
+ int security_key_permission(key_ref_t key_ref, const struct cred *cred,
+                           enum key_need_perm need_perm)
  {
-       return call_int_hook(key_permission, 0, key_ref, cred, perm);
+       return call_int_hook(key_permission, 0, key_ref, cred, need_perm);
  }
  
  int security_key_getsecurity(struct key *key, char **_buffer)
diff --combined security/selinux/hooks.c
@@@ -2286,7 -2286,7 +2286,7 @@@ static int check_nnp_nosuid(const struc
        return -EACCES;
  }
  
 -static int selinux_bprm_set_creds(struct linux_binprm *bprm)
 +static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
  {
        const struct task_security_struct *old_tsec;
        struct task_security_struct *new_tsec;
  
        /* SELinux context only depends on initial program or script and not
         * the script interpreter */
 -      if (bprm->called_set_creds)
 -              return 0;
  
        old_tsec = selinux_cred(current_cred());
        new_tsec = selinux_cred(bprm->cred);
@@@ -6403,7 -6405,7 +6403,7 @@@ static int selinux_setprocattr(const ch
        /* Permission checking based on the specified context is
           performed during the actual operation (execve,
           open/mkdir/...), when we know the full context of the
 -         operation.  See selinux_bprm_set_creds for the execve
 +         operation.  See selinux_bprm_creds_for_exec for the execve
           checks and may_create for the file creation checks. The
           operation will then fail if the context is not permitted. */
        tsec = selinux_cred(new);
@@@ -6559,20 -6561,43 +6559,43 @@@ static void selinux_key_free(struct ke
  
  static int selinux_key_permission(key_ref_t key_ref,
                                  const struct cred *cred,
-                                 unsigned perm)
+                                 enum key_need_perm need_perm)
  {
        struct key *key;
        struct key_security_struct *ksec;
-       u32 sid;
+       u32 perm, sid;
  
-       /* if no specific permissions are requested, we skip the
-          permission check. No serious, additional covert channels
-          appear to be created. */
-       if (perm == 0)
+       switch (need_perm) {
+       case KEY_NEED_VIEW:
+               perm = KEY__VIEW;
+               break;
+       case KEY_NEED_READ:
+               perm = KEY__READ;
+               break;
+       case KEY_NEED_WRITE:
+               perm = KEY__WRITE;
+               break;
+       case KEY_NEED_SEARCH:
+               perm = KEY__SEARCH;
+               break;
+       case KEY_NEED_LINK:
+               perm = KEY__LINK;
+               break;
+       case KEY_NEED_SETATTR:
+               perm = KEY__SETATTR;
+               break;
+       case KEY_NEED_UNLINK:
+       case KEY_SYSADMIN_OVERRIDE:
+       case KEY_AUTHTOKEN_OVERRIDE:
+       case KEY_DEFER_PERM_CHECK:
                return 0;
+       default:
+               WARN_ON(1);
+               return -EPERM;
  
-       sid = cred_sid(cred);
+       }
  
+       sid = cred_sid(cred);
        key = key_ref_to_ptr(key_ref);
        ksec = key->security;
  
@@@ -6594,6 -6619,17 +6617,17 @@@ static int selinux_key_getsecurity(stru
        *_buffer = context;
        return rc;
  }
+ #ifdef CONFIG_KEY_NOTIFICATIONS
+ static int selinux_watch_key(struct key *key)
+ {
+       struct key_security_struct *ksec = key->security;
+       u32 sid = current_sid();
+       return avc_has_perm(&selinux_state,
+                           sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
+ }
+ #endif
  #endif
  
  #ifdef CONFIG_SECURITY_INFINIBAND
@@@ -6932,7 -6968,7 +6966,7 @@@ static struct security_hook_list selinu
  
        LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
  
 -      LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
 +      LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
        LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
        LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
  
        LSM_HOOK_INIT(key_free, selinux_key_free),
        LSM_HOOK_INIT(key_permission, selinux_key_permission),
        LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
+ #ifdef CONFIG_KEY_NOTIFICATIONS
+       LSM_HOOK_INIT(watch_key, selinux_watch_key),
+ #endif
  #endif
  
  #ifdef CONFIG_AUDIT
@@@ -41,6 -41,7 +41,7 @@@
  #include <linux/parser.h>
  #include <linux/fs_context.h>
  #include <linux/fs_parser.h>
+ #include <linux/watch_queue.h>
  #include "smack.h"
  
  #define TRANS_TRUE    "TRUE"
  #define SMK_RECEIVING 1
  #define SMK_SENDING   2
  
 -#ifdef SMACK_IPV6_PORT_LABELING
 -DEFINE_MUTEX(smack_ipv6_lock);
 +static DEFINE_MUTEX(smack_ipv6_lock);
  static LIST_HEAD(smk_ipv6_port_list);
 -#endif
 -static struct kmem_cache *smack_inode_cache;
  struct kmem_cache *smack_rule_cache;
  int smack_enabled;
  
@@@ -313,6 -317,7 +314,6 @@@ static void init_inode_smack(struct ino
  
        isp->smk_inode = skp;
        isp->smk_flags = 0;
 -      mutex_init(&isp->smk_lock);
  }
  
  /**
@@@ -887,12 -892,12 +888,12 @@@ static int smack_sb_statfs(struct dentr
   */
  
  /**
 - * smack_bprm_set_creds - set creds for exec
 + * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
   * @bprm: the exec information
   *
   * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
   */
 -static int smack_bprm_set_creds(struct linux_binprm *bprm)
 +static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
  {
        struct inode *inode = file_inode(bprm->file);
        struct task_smack *bsp = smack_cred(bprm->cred);
        struct superblock_smack *sbsp;
        int rc;
  
 -      if (bprm->called_set_creds)
 -              return 0;
 -
        isp = smack_inode(inode);
        if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
                return 0;
@@@ -2313,6 -2321,7 +2314,6 @@@ static struct smack_known *smack_ipv4ho
        return NULL;
  }
  
 -#if IS_ENABLED(CONFIG_IPV6)
  /*
   * smk_ipv6_localhost - Check for local ipv6 host address
   * @sip: the address
@@@ -2380,6 -2389,7 +2381,6 @@@ static struct smack_known *smack_ipv6ho
  
        return NULL;
  }
 -#endif /* CONFIG_IPV6 */
  
  /**
   * smack_netlabel - Set the secattr on a socket
@@@ -2468,6 -2478,7 +2469,6 @@@ static int smack_netlabel_send(struct s
        return smack_netlabel(sk, sk_lbl);
  }
  
 -#if IS_ENABLED(CONFIG_IPV6)
  /**
   * smk_ipv6_check - check Smack access
   * @subject: subject Smack label
@@@ -2500,6 -2511,7 +2501,6 @@@ static int smk_ipv6_check(struct smack_
        rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
        return rc;
  }
 -#endif /* CONFIG_IPV6 */
  
  #ifdef SMACK_IPV6_PORT_LABELING
  /**
@@@ -2588,7 -2600,6 +2589,7 @@@ static void smk_ipv6_port_label(struct 
        mutex_unlock(&smack_ipv6_lock);
        return;
  }
 +#endif
  
  /**
   * smk_ipv6_port_check - check Smack port access
@@@ -2651,6 -2662,7 +2652,6 @@@ static int smk_ipv6_port_check(struct s
  
        return smk_ipv6_check(skp, object, address, act);
  }
 -#endif /* SMACK_IPV6_PORT_LABELING */
  
  /**
   * smack_inode_setsecurity - set smack xattrs
@@@ -2825,21 -2837,24 +2826,21 @@@ static int smack_socket_connect(struct 
                return 0;
        if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
                struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
 -#ifdef SMACK_IPV6_SECMARK_LABELING
 -              struct smack_known *rsp;
 -#endif
 +              struct smack_known *rsp = NULL;
  
                if (addrlen < SIN6_LEN_RFC2133)
                        return 0;
 -#ifdef SMACK_IPV6_SECMARK_LABELING
 -              rsp = smack_ipv6host_label(sip);
 +              if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
 +                      rsp = smack_ipv6host_label(sip);
                if (rsp != NULL) {
                        struct socket_smack *ssp = sock->sk->sk_security;
  
                        rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
                                            SMK_CONNECTING);
                }
 -#endif
 -#ifdef SMACK_IPV6_PORT_LABELING
 -              rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
 -#endif
 +              if (__is_defined(SMACK_IPV6_PORT_LABELING))
 +                      rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
 +
                return rc;
        }
        if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
@@@ -3259,12 -3274,13 +3260,12 @@@ static void smack_d_instantiate(struct 
  
        isp = smack_inode(inode);
  
 -      mutex_lock(&isp->smk_lock);
        /*
         * If the inode is already instantiated
         * take the quick way out
         */
        if (isp->smk_flags & SMK_INODE_INSTANT)
 -              goto unlockandout;
 +              return;
  
        sbp = inode->i_sb;
        sbsp = sbp->s_security;
                        break;
                }
                isp->smk_flags |= SMK_INODE_INSTANT;
 -              goto unlockandout;
 +              return;
        }
  
        /*
  
        isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
  
 -unlockandout:
 -      mutex_unlock(&isp->smk_lock);
        return;
  }
  
@@@ -4213,13 -4231,14 +4214,14 @@@ static void smack_key_free(struct key *
   * smack_key_permission - Smack access on a key
   * @key_ref: gets to the object
   * @cred: the credentials to use
-  * @perm: requested key permissions
+  * @need_perm: requested key permission
   *
   * Return 0 if the task has read and write to the object,
   * an error code otherwise
   */
  static int smack_key_permission(key_ref_t key_ref,
-                               const struct cred *cred, unsigned perm)
+                               const struct cred *cred,
+                               enum key_need_perm need_perm)
  {
        struct key *keyp;
        struct smk_audit_info ad;
        /*
         * Validate requested permissions
         */
-       if (perm & ~KEY_NEED_ALL)
+       switch (need_perm) {
+       case KEY_NEED_READ:
+       case KEY_NEED_SEARCH:
+       case KEY_NEED_VIEW:
+               request |= MAY_READ;
+               break;
+       case KEY_NEED_WRITE:
+       case KEY_NEED_LINK:
+       case KEY_NEED_SETATTR:
+               request |= MAY_WRITE;
+               break;
+       case KEY_NEED_UNSPECIFIED:
+       case KEY_NEED_UNLINK:
+       case KEY_SYSADMIN_OVERRIDE:
+       case KEY_AUTHTOKEN_OVERRIDE:
+       case KEY_DEFER_PERM_CHECK:
+               return 0;
+       default:
                return -EINVAL;
+       }
  
        keyp = key_ref_to_ptr(key_ref);
        if (keyp == NULL)
        if (tkp == NULL)
                return -EACCES;
  
-       if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
+       if (smack_privileged(CAP_MAC_OVERRIDE))
                return 0;
  
  #ifdef CONFIG_AUDIT
        ad.a.u.key_struct.key = keyp->serial;
        ad.a.u.key_struct.key_desc = keyp->description;
  #endif
-       if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
-               request |= MAY_READ;
-       if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
-               request |= MAY_WRITE;
        rc = smk_access(tkp, keyp->security, request, &ad);
        rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
        return rc;
@@@ -4294,8 -4327,81 +4310,81 @@@ static int smack_key_getsecurity(struc
        return length;
  }
  
+ #ifdef CONFIG_KEY_NOTIFICATIONS
+ /**
+  * smack_watch_key - Smack access to watch a key for notifications.
+  * @key: The key to be watched
+  *
+  * Return 0 if the @watch->cred has permission to read from the key object and
+  * an error otherwise.
+  */
+ static int smack_watch_key(struct key *key)
+ {
+       struct smk_audit_info ad;
+       struct smack_known *tkp = smk_of_current();
+       int rc;
+       if (key == NULL)
+               return -EINVAL;
+       /*
+        * If the key hasn't been initialized give it access so that
+        * it may do so.
+        */
+       if (key->security == NULL)
+               return 0;
+       /*
+        * This should not occur
+        */
+       if (tkp == NULL)
+               return -EACCES;
+       if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
+               return 0;
+ #ifdef CONFIG_AUDIT
+       smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
+       ad.a.u.key_struct.key = key->serial;
+       ad.a.u.key_struct.key_desc = key->description;
+ #endif
+       rc = smk_access(tkp, key->security, MAY_READ, &ad);
+       rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
+       return rc;
+ }
+ #endif /* CONFIG_KEY_NOTIFICATIONS */
  #endif /* CONFIG_KEYS */
  
+ #ifdef CONFIG_WATCH_QUEUE
+ /**
+  * smack_post_notification - Smack access to post a notification to a queue
+  * @w_cred: The credentials of the watcher.
+  * @cred: The credentials of the event source (may be NULL).
+  * @n: The notification message to be posted.
+  */
+ static int smack_post_notification(const struct cred *w_cred,
+                                  const struct cred *cred,
+                                  struct watch_notification *n)
+ {
+       struct smk_audit_info ad;
+       struct smack_known *subj, *obj;
+       int rc;
+       /* Always let maintenance notifications through. */
+       if (n->type == WATCH_TYPE_META)
+               return 0;
+       if (!cred)
+               return 0;
+       subj = smk_of_task(smack_cred(cred));
+       obj = smk_of_task(smack_cred(w_cred));
+       smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
+       rc = smk_access(subj, obj, MAY_WRITE, &ad);
+       rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
+       return rc;
+ }
+ #endif /* CONFIG_WATCH_QUEUE */
  /*
   * Smack Audit hooks
   *
@@@ -4581,7 -4687,7 +4670,7 @@@ static struct security_hook_list smack_
        LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
        LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
  
 -      LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
 +      LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
  
        LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
        LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
        LSM_HOOK_INIT(key_free, smack_key_free),
        LSM_HOOK_INIT(key_permission, smack_key_permission),
        LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
+ #ifdef CONFIG_KEY_NOTIFICATIONS
+       LSM_HOOK_INIT(watch_key, smack_watch_key),
+ #endif
  #endif /* CONFIG_KEYS */
  
+ #ifdef CONFIG_WATCH_QUEUE
+       LSM_HOOK_INIT(post_notification, smack_post_notification),
+ #endif
   /* Audit hooks */
  #ifdef CONFIG_AUDIT
        LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
@@@ -4743,9 -4856,15 +4839,9 @@@ static __init int smack_init(void
        struct cred *cred = (struct cred *) current->cred;
        struct task_smack *tsp;
  
 -      smack_inode_cache = KMEM_CACHE(inode_smack, 0);
 -      if (!smack_inode_cache)
 -              return -ENOMEM;
 -
        smack_rule_cache = KMEM_CACHE(smack_rule, 0);
 -      if (!smack_rule_cache) {
 -              kmem_cache_destroy(smack_inode_cache);
 +      if (!smack_rule_cache)
                return -ENOMEM;
 -      }
  
        /*
         * Set the security state for the initial task.