Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse

Pull fuse updates from Miklos Szeredi:
 "Support for pid namespaces from Seth and refcount_t work from Elena"

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse:
  fuse: Add support for pid namespaces
  fuse: convert fuse_conn.count from atomic_t to refcount_t
  fuse: convert fuse_req.count from atomic_t to refcount_t
  fuse: convert fuse_file.count from atomic_t to refcount_t
This commit is contained in:
Linus Torvalds 2017-05-10 08:45:30 -07:00
commit a2e5ad45a9
4 changed files with 50 additions and 24 deletions

View file

@ -20,6 +20,7 @@
#include <linux/pipe_fs_i.h> #include <linux/pipe_fs_i.h>
#include <linux/swap.h> #include <linux/swap.h>
#include <linux/splice.h> #include <linux/splice.h>
#include <linux/sched.h>
MODULE_ALIAS_MISCDEV(FUSE_MINOR); MODULE_ALIAS_MISCDEV(FUSE_MINOR);
MODULE_ALIAS("devname:fuse"); MODULE_ALIAS("devname:fuse");
@ -45,7 +46,7 @@ static void fuse_request_init(struct fuse_req *req, struct page **pages,
INIT_LIST_HEAD(&req->list); INIT_LIST_HEAD(&req->list);
INIT_LIST_HEAD(&req->intr_entry); INIT_LIST_HEAD(&req->intr_entry);
init_waitqueue_head(&req->waitq); init_waitqueue_head(&req->waitq);
atomic_set(&req->count, 1); refcount_set(&req->count, 1);
req->pages = pages; req->pages = pages;
req->page_descs = page_descs; req->page_descs = page_descs;
req->max_pages = npages; req->max_pages = npages;
@ -102,21 +103,20 @@ void fuse_request_free(struct fuse_req *req)
void __fuse_get_request(struct fuse_req *req) void __fuse_get_request(struct fuse_req *req)
{ {
atomic_inc(&req->count); refcount_inc(&req->count);
} }
/* Must be called with > 1 refcount */ /* Must be called with > 1 refcount */
static void __fuse_put_request(struct fuse_req *req) static void __fuse_put_request(struct fuse_req *req)
{ {
BUG_ON(atomic_read(&req->count) < 2); refcount_dec(&req->count);
atomic_dec(&req->count);
} }
static void fuse_req_init_context(struct fuse_req *req) static void fuse_req_init_context(struct fuse_conn *fc, struct fuse_req *req)
{ {
req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid()); req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid()); req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
req->in.h.pid = current->pid; req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
} }
void fuse_set_initialized(struct fuse_conn *fc) void fuse_set_initialized(struct fuse_conn *fc)
@ -163,7 +163,7 @@ static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
goto out; goto out;
} }
fuse_req_init_context(req); fuse_req_init_context(fc, req);
__set_bit(FR_WAITING, &req->flags); __set_bit(FR_WAITING, &req->flags);
if (for_background) if (for_background)
__set_bit(FR_BACKGROUND, &req->flags); __set_bit(FR_BACKGROUND, &req->flags);
@ -256,7 +256,7 @@ struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
if (!req) if (!req)
req = get_reserved_req(fc, file); req = get_reserved_req(fc, file);
fuse_req_init_context(req); fuse_req_init_context(fc, req);
__set_bit(FR_WAITING, &req->flags); __set_bit(FR_WAITING, &req->flags);
__clear_bit(FR_BACKGROUND, &req->flags); __clear_bit(FR_BACKGROUND, &req->flags);
return req; return req;
@ -264,7 +264,7 @@ struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
{ {
if (atomic_dec_and_test(&req->count)) { if (refcount_dec_and_test(&req->count)) {
if (test_bit(FR_BACKGROUND, &req->flags)) { if (test_bit(FR_BACKGROUND, &req->flags)) {
/* /*
* We get here in the unlikely case that a background * We get here in the unlikely case that a background
@ -1222,6 +1222,9 @@ static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
struct fuse_in *in; struct fuse_in *in;
unsigned reqsize; unsigned reqsize;
if (task_active_pid_ns(current) != fc->pid_ns)
return -EIO;
restart: restart:
spin_lock(&fiq->waitq.lock); spin_lock(&fiq->waitq.lock);
err = -EAGAIN; err = -EAGAIN;
@ -1820,6 +1823,9 @@ static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
struct fuse_req *req; struct fuse_req *req;
struct fuse_out_header oh; struct fuse_out_header oh;
if (task_active_pid_ns(current) != fc->pid_ns)
return -EIO;
if (nbytes < sizeof(struct fuse_out_header)) if (nbytes < sizeof(struct fuse_out_header))
return -EINVAL; return -EINVAL;

View file

@ -58,7 +58,7 @@ struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
} }
INIT_LIST_HEAD(&ff->write_entry); INIT_LIST_HEAD(&ff->write_entry);
atomic_set(&ff->count, 1); refcount_set(&ff->count, 1);
RB_CLEAR_NODE(&ff->polled_node); RB_CLEAR_NODE(&ff->polled_node);
init_waitqueue_head(&ff->poll_wait); init_waitqueue_head(&ff->poll_wait);
@ -77,7 +77,7 @@ void fuse_file_free(struct fuse_file *ff)
static struct fuse_file *fuse_file_get(struct fuse_file *ff) static struct fuse_file *fuse_file_get(struct fuse_file *ff)
{ {
atomic_inc(&ff->count); refcount_inc(&ff->count);
return ff; return ff;
} }
@ -88,7 +88,7 @@ static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
static void fuse_file_put(struct fuse_file *ff, bool sync) static void fuse_file_put(struct fuse_file *ff, bool sync)
{ {
if (atomic_dec_and_test(&ff->count)) { if (refcount_dec_and_test(&ff->count)) {
struct fuse_req *req = ff->reserved_req; struct fuse_req *req = ff->reserved_req;
if (ff->fc->no_open) { if (ff->fc->no_open) {
@ -293,7 +293,7 @@ static int fuse_release(struct inode *inode, struct file *file)
void fuse_sync_release(struct fuse_file *ff, int flags) void fuse_sync_release(struct fuse_file *ff, int flags)
{ {
WARN_ON(atomic_read(&ff->count) != 1); WARN_ON(refcount_read(&ff->count) > 1);
fuse_prepare_release(ff, flags, FUSE_RELEASE); fuse_prepare_release(ff, flags, FUSE_RELEASE);
/* /*
* iput(NULL) is a no-op and since the refcount is 1 and everything's * iput(NULL) is a no-op and since the refcount is 1 and everything's
@ -2083,7 +2083,8 @@ static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
return generic_file_mmap(file, vma); return generic_file_mmap(file, vma);
} }
static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, static int convert_fuse_file_lock(struct fuse_conn *fc,
const struct fuse_file_lock *ffl,
struct file_lock *fl) struct file_lock *fl)
{ {
switch (ffl->type) { switch (ffl->type) {
@ -2098,7 +2099,14 @@ static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
fl->fl_start = ffl->start; fl->fl_start = ffl->start;
fl->fl_end = ffl->end; fl->fl_end = ffl->end;
fl->fl_pid = ffl->pid;
/*
* Convert pid into the caller's pid namespace. If the pid
* does not map into the namespace fl_pid will get set to 0.
*/
rcu_read_lock();
fl->fl_pid = pid_vnr(find_pid_ns(ffl->pid, fc->pid_ns));
rcu_read_unlock();
break; break;
default: default:
@ -2147,7 +2155,7 @@ static int fuse_getlk(struct file *file, struct file_lock *fl)
args.out.args[0].value = &outarg; args.out.args[0].value = &outarg;
err = fuse_simple_request(fc, &args); err = fuse_simple_request(fc, &args);
if (!err) if (!err)
err = convert_fuse_file_lock(&outarg.lk, fl); err = convert_fuse_file_lock(fc, &outarg.lk, fl);
return err; return err;
} }
@ -2159,7 +2167,8 @@ static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
FUSE_ARGS(args); FUSE_ARGS(args);
struct fuse_lk_in inarg; struct fuse_lk_in inarg;
int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
int err; int err;
if (fl->fl_lmops && fl->fl_lmops->lm_grant) { if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
@ -2171,7 +2180,10 @@ static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
if (fl->fl_flags & FL_CLOSE) if (fl->fl_flags & FL_CLOSE)
return 0; return 0;
fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg); if (pid && pid_nr == 0)
return -EOVERFLOW;
fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
err = fuse_simple_request(fc, &args); err = fuse_simple_request(fc, &args);
/* locking is restartable */ /* locking is restartable */

View file

@ -24,6 +24,8 @@
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <linux/kref.h> #include <linux/kref.h>
#include <linux/xattr.h> #include <linux/xattr.h>
#include <linux/pid_namespace.h>
#include <linux/refcount.h>
/** Max number of pages that can be used in a single read request */ /** Max number of pages that can be used in a single read request */
#define FUSE_MAX_PAGES_PER_REQ 32 #define FUSE_MAX_PAGES_PER_REQ 32
@ -137,7 +139,7 @@ struct fuse_file {
u64 nodeid; u64 nodeid;
/** Refcount */ /** Refcount */
atomic_t count; refcount_t count;
/** FOPEN_* flags returned by open */ /** FOPEN_* flags returned by open */
u32 open_flags; u32 open_flags;
@ -306,7 +308,7 @@ struct fuse_req {
struct list_head intr_entry; struct list_head intr_entry;
/** refcount */ /** refcount */
atomic_t count; refcount_t count;
/** Unique ID for the interrupt request */ /** Unique ID for the interrupt request */
u64 intr_unique; u64 intr_unique;
@ -448,7 +450,7 @@ struct fuse_conn {
spinlock_t lock; spinlock_t lock;
/** Refcount */ /** Refcount */
atomic_t count; refcount_t count;
/** Number of fuse_dev's */ /** Number of fuse_dev's */
atomic_t dev_count; atomic_t dev_count;
@ -461,6 +463,9 @@ struct fuse_conn {
/** The group id for this mount */ /** The group id for this mount */
kgid_t group_id; kgid_t group_id;
/** The pid namespace for this mount */
struct pid_namespace *pid_ns;
/** Maximum read size */ /** Maximum read size */
unsigned max_read; unsigned max_read;

View file

@ -21,6 +21,7 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/exportfs.h> #include <linux/exportfs.h>
#include <linux/posix_acl.h> #include <linux/posix_acl.h>
#include <linux/pid_namespace.h>
MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
MODULE_DESCRIPTION("Filesystem in Userspace"); MODULE_DESCRIPTION("Filesystem in Userspace");
@ -601,7 +602,7 @@ void fuse_conn_init(struct fuse_conn *fc)
memset(fc, 0, sizeof(*fc)); memset(fc, 0, sizeof(*fc));
spin_lock_init(&fc->lock); spin_lock_init(&fc->lock);
init_rwsem(&fc->killsb); init_rwsem(&fc->killsb);
atomic_set(&fc->count, 1); refcount_set(&fc->count, 1);
atomic_set(&fc->dev_count, 1); atomic_set(&fc->dev_count, 1);
init_waitqueue_head(&fc->blocked_waitq); init_waitqueue_head(&fc->blocked_waitq);
init_waitqueue_head(&fc->reserved_req_waitq); init_waitqueue_head(&fc->reserved_req_waitq);
@ -619,14 +620,16 @@ void fuse_conn_init(struct fuse_conn *fc)
fc->connected = 1; fc->connected = 1;
fc->attr_version = 1; fc->attr_version = 1;
get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key)); get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
} }
EXPORT_SYMBOL_GPL(fuse_conn_init); EXPORT_SYMBOL_GPL(fuse_conn_init);
void fuse_conn_put(struct fuse_conn *fc) void fuse_conn_put(struct fuse_conn *fc)
{ {
if (atomic_dec_and_test(&fc->count)) { if (refcount_dec_and_test(&fc->count)) {
if (fc->destroy_req) if (fc->destroy_req)
fuse_request_free(fc->destroy_req); fuse_request_free(fc->destroy_req);
put_pid_ns(fc->pid_ns);
fc->release(fc); fc->release(fc);
} }
} }
@ -634,7 +637,7 @@ EXPORT_SYMBOL_GPL(fuse_conn_put);
struct fuse_conn *fuse_conn_get(struct fuse_conn *fc) struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
{ {
atomic_inc(&fc->count); refcount_inc(&fc->count);
return fc; return fc;
} }
EXPORT_SYMBOL_GPL(fuse_conn_get); EXPORT_SYMBOL_GPL(fuse_conn_get);