Skip to content

Commit 2f62b5a

Browse files
ereshetovaamschuma-ntap
authored andcommitted
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: Kees Cook <keescook@chromium.org> Reviewed-by: David Windsor <dwindsor@gmail.com> Reviewed-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
1 parent 194bc1f commit 2f62b5a

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

fs/nfs/inode.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ EXPORT_SYMBOL_GPL(nfs_getattr);
783783

784784
static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
785785
{
786-
atomic_set(&l_ctx->count, 1);
786+
refcount_set(&l_ctx->count, 1);
787787
l_ctx->lockowner = current->files;
788788
INIT_LIST_HEAD(&l_ctx->list);
789789
atomic_set(&l_ctx->io_count, 0);
@@ -797,7 +797,7 @@ static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context
797797
do {
798798
if (pos->lockowner != current->files)
799799
continue;
800-
atomic_inc(&pos->count);
800+
refcount_inc(&pos->count);
801801
return pos;
802802
} while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head);
803803
return NULL;
@@ -836,7 +836,7 @@ void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
836836
struct nfs_open_context *ctx = l_ctx->open_context;
837837
struct inode *inode = d_inode(ctx->dentry);
838838

839-
if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock))
839+
if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock))
840840
return;
841841
list_del(&l_ctx->list);
842842
spin_unlock(&inode->i_lock);
@@ -913,7 +913,7 @@ EXPORT_SYMBOL_GPL(alloc_nfs_open_context);
913913
struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
914914
{
915915
if (ctx != NULL)
916-
atomic_inc(&ctx->lock_context.count);
916+
refcount_inc(&ctx->lock_context.count);
917917
return ctx;
918918
}
919919
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)
924924
struct super_block *sb = ctx->dentry->d_sb;
925925

926926
if (!list_empty(&ctx->list)) {
927-
if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
927+
if (!refcount_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
928928
return;
929929
list_del(&ctx->list);
930930
spin_unlock(&inode->i_lock);
931-
} else if (!atomic_dec_and_test(&ctx->lock_context.count))
931+
} else if (!refcount_dec_and_test(&ctx->lock_context.count))
932932
return;
933933
if (inode != NULL)
934934
NFS_PROTO(inode)->close_context(ctx, is_sync);

include/linux/nfs_fs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/mm.h>
2323
#include <linux/pagemap.h>
2424
#include <linux/rbtree.h>
25+
#include <linux/refcount.h>
2526
#include <linux/rwsem.h>
2627
#include <linux/wait.h>
2728

@@ -55,7 +56,7 @@ struct nfs_access_entry {
5556
};
5657

5758
struct nfs_lock_context {
58-
atomic_t count;
59+
refcount_t count;
5960
struct list_head list;
6061
struct nfs_open_context *open_context;
6162
fl_owner_t lockowner;

0 commit comments

Comments
 (0)