Merge tag 'binfmt-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb...
authorLinus Torvalds <torvalds@linux-foundation.org>
Sun, 7 Aug 2016 14:13:14 +0000 (10:13 -0400)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sun, 7 Aug 2016 14:13:14 +0000 (10:13 -0400)
Pull binfmt_misc update from James Bottomley:
 "This update is to allow architecture emulation containers to function
  such that the emulation binary can be housed outside the container
  itself.  The container and fs parts both have acks from relevant
  experts.

  To use the new feature you have to add an F option to your binfmt_misc
  configuration"

From the docs:
 "The usual behaviour of binfmt_misc is to spawn the binary lazily when
  the misc format file is invoked.  However, this doesn't work very well
  in the face of mount namespaces and changeroots, so the F mode opens
  the binary as soon as the emulation is installed and uses the opened
  image to spawn the emulator, meaning it is always available once
  installed, regardless of how the environment changes"

* tag 'binfmt-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/binfmt_misc:
  binfmt_misc: add F option description to documentation
  binfmt_misc: add persistent opened binary handler for containers
  fs: add filp_clone_open API

1  2 
fs/binfmt_misc.c
fs/internal.h
fs/open.c

diff --combined fs/binfmt_misc.c
@@@ -26,6 -26,8 +26,8 @@@
  #include <linux/fs.h>
  #include <linux/uaccess.h>
  
+ #include "internal.h"
  #ifdef DEBUG
  # define USE_DEBUG 1
  #else
@@@ -43,6 -45,7 +45,7 @@@ enum {Enabled, Magic}
  #define MISC_FMT_PRESERVE_ARGV0 (1 << 31)
  #define MISC_FMT_OPEN_BINARY (1 << 30)
  #define MISC_FMT_CREDENTIALS (1 << 29)
+ #define MISC_FMT_OPEN_FILE (1 << 28)
  
  typedef struct {
        struct list_head list;
@@@ -54,6 -57,7 +57,7 @@@
        char *interpreter;              /* filename of interpreter */
        char *name;
        struct dentry *dentry;
+       struct file *interp_file;
  } Node;
  
  static DEFINE_RWLOCK(entries_lock);
@@@ -201,7 -205,13 +205,13 @@@ static int load_misc_binary(struct linu
        if (retval < 0)
                goto error;
  
-       interp_file = open_exec(iname);
+       if (fmt->flags & MISC_FMT_OPEN_FILE && fmt->interp_file) {
+               interp_file = filp_clone_open(fmt->interp_file);
+               if (!IS_ERR(interp_file))
+                       deny_write_access(interp_file);
+       } else {
+               interp_file = open_exec(iname);
+       }
        retval = PTR_ERR(interp_file);
        if (IS_ERR(interp_file))
                goto error;
@@@ -285,6 -295,11 +295,11 @@@ static char *check_special_flags(char *
                        e->flags |= (MISC_FMT_CREDENTIALS |
                                        MISC_FMT_OPEN_BINARY);
                        break;
+               case 'F':
+                       pr_debug("register: flag: F: open interpreter file now\n");
+                       p++;
+                       e->flags |= MISC_FMT_OPEN_FILE;
+                       break;
                default:
                        cont = 0;
                }
@@@ -543,6 -558,8 +558,8 @@@ static void entry_status(Node *e, char 
                *dp++ = 'O';
        if (e->flags & MISC_FMT_CREDENTIALS)
                *dp++ = 'C';
+       if (e->flags & MISC_FMT_OPEN_FILE)
+               *dp++ = 'F';
        *dp++ = '\n';
  
        if (!test_bit(Magic, &e->flags)) {
@@@ -590,6 -607,11 +607,11 @@@ static void kill_node(Node *e
        }
        write_unlock(&entries_lock);
  
+       if ((e->flags & MISC_FMT_OPEN_FILE) && e->interp_file) {
+               filp_close(e->interp_file, NULL);
+               e->interp_file = NULL;
+       }
        if (dentry) {
                drop_nlink(d_inode(dentry));
                d_drop(dentry);
@@@ -637,12 -659,13 +659,12 @@@ static ssize_t bm_entry_write(struct fi
                break;
        case 3:
                /* Delete this handler. */
 -              root = dget(file->f_path.dentry->d_sb->s_root);
 +              root = file_inode(file)->i_sb->s_root;
                inode_lock(d_inode(root));
  
                kill_node(e);
  
                inode_unlock(d_inode(root));
 -              dput(root);
                break;
        default:
                return res;
@@@ -664,8 -687,8 +686,8 @@@ static ssize_t bm_register_write(struc
  {
        Node *e;
        struct inode *inode;
 -      struct dentry *root, *dentry;
 -      struct super_block *sb = file->f_path.dentry->d_sb;
 +      struct super_block *sb = file_inode(file)->i_sb;
 +      struct dentry *root = sb->s_root, *dentry;
        int err = 0;
  
        e = create_entry(buffer, count);
        if (IS_ERR(e))
                return PTR_ERR(e);
  
 -      root = dget(sb->s_root);
        inode_lock(d_inode(root));
        dentry = lookup_one_len(e->name, root, strlen(e->name));
        err = PTR_ERR(dentry);
                goto out2;
        }
  
+       if (e->flags & MISC_FMT_OPEN_FILE) {
+               struct file *f;
+               f = open_exec(e->interpreter);
+               if (IS_ERR(f)) {
+                       err = PTR_ERR(f);
+                       pr_notice("register: failed to install interpreter file %s\n", e->interpreter);
+                       simple_release_fs(&bm_mnt, &entry_count);
+                       iput(inode);
+                       inode = NULL;
+                       goto out2;
+               }
+               e->interp_file = f;
+       }
        e->dentry = dget(dentry);
        inode->i_private = e;
        inode->i_fop = &bm_entry_operations;
@@@ -710,10 -749,11 +747,10 @@@ out2
        dput(dentry);
  out:
        inode_unlock(d_inode(root));
 -      dput(root);
  
        if (err) {
                kfree(e);
-               return -EINVAL;
+               return err;
        }
        return count;
  }
@@@ -750,13 -790,14 +787,13 @@@ static ssize_t bm_status_write(struct f
                break;
        case 3:
                /* Delete all handlers. */
 -              root = dget(file->f_path.dentry->d_sb->s_root);
 +              root = file_inode(file)->i_sb->s_root;
                inode_lock(d_inode(root));
  
                while (!list_empty(&entries))
                        kill_node(list_entry(entries.next, Node, list));
  
                inode_unlock(d_inode(root));
 -              dput(root);
                break;
        default:
                return res;
diff --combined fs/internal.h
@@@ -11,7 -11,6 +11,7 @@@
  
  struct super_block;
  struct file_system_type;
 +struct iomap;
  struct linux_binprm;
  struct path;
  struct mount;
@@@ -40,8 -39,6 +40,8 @@@ static inline int __sync_blockdev(struc
   * buffer.c
   */
  extern void guard_bio_eod(int rw, struct bio *bio);
 +extern int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
 +              get_block_t *get_block, struct iomap *iomap);
  
  /*
   * char_dev.c
@@@ -111,13 -108,13 +111,14 @@@ extern long do_handle_open(int mountdir
                           struct file_handle __user *ufh, int open_flag);
  extern int open_check_o_direct(struct file *f);
  extern int vfs_open(const struct path *, struct file *, const struct cred *);
+ extern struct file *filp_clone_open(struct file *);
  
  /*
   * inode.c
   */
  extern long prune_icache_sb(struct super_block *sb, struct shrink_control *sc);
  extern void inode_add_lru(struct inode *inode);
 +extern int dentry_needs_remove_privs(struct dentry *dentry);
  
  /*
   * fs-writeback.c
@@@ -134,7 -131,6 +135,7 @@@ extern int invalidate_inodes(struct sup
  extern struct dentry *__d_alloc(struct super_block *, const struct qstr *);
  extern int d_set_mounted(struct dentry *dentry);
  extern long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc);
 +extern struct dentry *d_alloc_cursor(struct dentry *);
  
  /*
   * read_write.c
diff --combined fs/open.c
+++ b/fs/open.c
@@@ -65,7 -65,7 +65,7 @@@ int do_truncate(struct dentry *dentry, 
        return ret;
  }
  
 -long vfs_truncate(struct path *path, loff_t length)
 +long vfs_truncate(const struct path *path, loff_t length)
  {
        struct inode *inode;
        long error;
@@@ -499,7 -499,7 +499,7 @@@ out
        return error;
  }
  
 -static int chmod_common(struct path *path, umode_t mode)
 +static int chmod_common(const struct path *path, umode_t mode)
  {
        struct inode *inode = path->dentry->d_inode;
        struct inode *delegated_inode = NULL;
@@@ -564,7 -564,7 +564,7 @@@ SYSCALL_DEFINE2(chmod, const char __use
        return sys_fchmodat(AT_FDCWD, filename, mode);
  }
  
 -static int chown_common(struct path *path, uid_t user, gid_t group)
 +static int chown_common(const struct path *path, uid_t user, gid_t group)
  {
        struct inode *inode = path->dentry->d_inode;
        struct inode *delegated_inode = NULL;
@@@ -713,7 -713,7 +713,7 @@@ static int do_dentry_open(struct file *
        }
  
        /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
 -      if (S_ISREG(inode->i_mode))
 +      if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
                f->f_mode |= FMODE_ATOMIC_POS;
  
        f->f_op = fops_get(inode->i_fop);
@@@ -840,13 -840,17 +840,13 @@@ EXPORT_SYMBOL(file_path)
  int vfs_open(const struct path *path, struct file *file,
             const struct cred *cred)
  {
 -      struct dentry *dentry = path->dentry;
 -      struct inode *inode = dentry->d_inode;
 +      struct dentry *dentry = d_real(path->dentry, NULL, file->f_flags);
  
 -      file->f_path = *path;
 -      if (dentry->d_flags & DCACHE_OP_SELECT_INODE) {
 -              inode = dentry->d_op->d_select_inode(dentry, file->f_flags);
 -              if (IS_ERR(inode))
 -                      return PTR_ERR(inode);
 -      }
 +      if (IS_ERR(dentry))
 +              return PTR_ERR(dentry);
  
 -      return do_dentry_open(file, inode, NULL, cred);
 +      file->f_path = *path;
 +      return do_dentry_open(file, d_backing_inode(dentry), NULL, cred);
  }
  
  struct file *dentry_open(const struct path *path, int flags,
@@@ -998,6 -1002,26 +998,26 @@@ struct file *file_open_root(struct dent
  }
  EXPORT_SYMBOL(file_open_root);
  
+ struct file *filp_clone_open(struct file *oldfile)
+ {
+       struct file *file;
+       int retval;
+       file = get_empty_filp();
+       if (IS_ERR(file))
+               return file;
+       file->f_flags = oldfile->f_flags;
+       retval = vfs_open(&oldfile->f_path, file, oldfile->f_cred);
+       if (retval) {
+               put_filp(file);
+               return ERR_PTR(retval);
+       }
+       return file;
+ }
+ EXPORT_SYMBOL(filp_clone_open);
  long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  {
        struct open_flags op;