Commit 2f62b5aa authored by Elena Reshetova's avatar Elena Reshetova Committed by Anna Schumaker

fs, nfs: convert nfs_lock_context.count from atomic_t to refcount_t

atomic_t variables are currently used to implement reference
counters with the following properties:
 - counter is initialized to 1 using atomic_set()
 - a resource is freed upon counter reaching zero
 - once counter reaches zero, its further
   increments aren't allowed
 - counter schema uses basic atomic operations
   (set, inc, inc_not_zero, dec_and_test, etc.)

Such atomic variables should be converted to a newly provided
refcount_t type and API that prevents accidental counter overflows
and underflows. This is important since overflows and underflows
can lead to use-after-free situation and be exploitable.

The variable nfs_lock_context.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.
Suggested-by: default avatarKees Cook <keescook@chromium.org>
Reviewed-by: default avatarDavid Windsor <dwindsor@gmail.com>
Reviewed-by: default avatarHans Liljestrand <ishkamiel@gmail.com>
Signed-off-by: default avatarElena Reshetova <elena.reshetova@intel.com>
Signed-off-by: default avatarAnna Schumaker <Anna.Schumaker@Netapp.com>
parent 194bc1f4
...@@ -783,7 +783,7 @@ EXPORT_SYMBOL_GPL(nfs_getattr); ...@@ -783,7 +783,7 @@ EXPORT_SYMBOL_GPL(nfs_getattr);
static void nfs_init_lock_context(struct nfs_lock_context *l_ctx) static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
{ {
atomic_set(&l_ctx->count, 1); refcount_set(&l_ctx->count, 1);
l_ctx->lockowner = current->files; l_ctx->lockowner = current->files;
INIT_LIST_HEAD(&l_ctx->list); INIT_LIST_HEAD(&l_ctx->list);
atomic_set(&l_ctx->io_count, 0); atomic_set(&l_ctx->io_count, 0);
...@@ -797,7 +797,7 @@ static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context ...@@ -797,7 +797,7 @@ static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context
do { do {
if (pos->lockowner != current->files) if (pos->lockowner != current->files)
continue; continue;
atomic_inc(&pos->count); refcount_inc(&pos->count);
return pos; return pos;
} while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head); } while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head);
return NULL; return NULL;
...@@ -836,7 +836,7 @@ void nfs_put_lock_context(struct nfs_lock_context *l_ctx) ...@@ -836,7 +836,7 @@ void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
struct nfs_open_context *ctx = l_ctx->open_context; struct nfs_open_context *ctx = l_ctx->open_context;
struct inode *inode = d_inode(ctx->dentry); struct inode *inode = d_inode(ctx->dentry);
if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock)) if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
return; return;
list_del(&l_ctx->list); list_del(&l_ctx->list);
spin_unlock(&inode->i_lock); spin_unlock(&inode->i_lock);
...@@ -913,7 +913,7 @@ EXPORT_SYMBOL_GPL(alloc_nfs_open_context); ...@@ -913,7 +913,7 @@ EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx) struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
{ {
if (ctx != NULL) if (ctx != NULL)
atomic_inc(&ctx->lock_context.count); refcount_inc(&ctx->lock_context.count);
return ctx; return ctx;
} }
EXPORT_SYMBOL_GPL(get_nfs_open_context); EXPORT_SYMBOL_GPL(get_nfs_open_context);
...@@ -924,11 +924,11 @@ static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync) ...@@ -924,11 +924,11 @@ static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
struct super_block *sb = ctx->dentry->d_sb; struct super_block *sb = ctx->dentry->d_sb;
if (!list_empty(&ctx->list)) { if (!list_empty(&ctx->list)) {
if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock)) if (!refcount_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
return; return;
list_del(&ctx->list); list_del(&ctx->list);
spin_unlock(&inode->i_lock); spin_unlock(&inode->i_lock);
} else if (!atomic_dec_and_test(&ctx->lock_context.count)) } else if (!refcount_dec_and_test(&ctx->lock_context.count))
return; return;
if (inode != NULL) if (inode != NULL)
NFS_PROTO(inode)->close_context(ctx, is_sync); NFS_PROTO(inode)->close_context(ctx, is_sync);
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/rbtree.h> #include <linux/rbtree.h>
#include <linux/refcount.h>
#include <linux/rwsem.h> #include <linux/rwsem.h>
#include <linux/wait.h> #include <linux/wait.h>
...@@ -55,7 +56,7 @@ struct nfs_access_entry { ...@@ -55,7 +56,7 @@ struct nfs_access_entry {
}; };
struct nfs_lock_context { struct nfs_lock_context {
atomic_t count; refcount_t count;
struct list_head list; struct list_head list;
struct nfs_open_context *open_context; struct nfs_open_context *open_context;
fl_owner_t lockowner; fl_owner_t lockowner;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment