Skip to content

Commit a2a5dea

Browse files
ereshetovaamschuma-ntap
authored andcommitted
fs, nfs: convert nfs4_pnfs_ds.ds_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 nfs4_pnfs_ds.ds_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 3be0f80 commit a2a5dea

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

fs/nfs/pnfs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#ifndef FS_NFS_PNFS_H
3131
#define FS_NFS_PNFS_H
3232

33+
#include <linux/refcount.h>
3334
#include <linux/nfs_fs.h>
3435
#include <linux/nfs_page.h>
3536
#include <linux/workqueue.h>
@@ -54,7 +55,7 @@ struct nfs4_pnfs_ds {
5455
char *ds_remotestr; /* comma sep list of addrs */
5556
struct list_head ds_addrs;
5657
struct nfs_client *ds_clp;
57-
atomic_t ds_count;
58+
refcount_t ds_count;
5859
unsigned long ds_state;
5960
#define NFS4DS_CONNECTING 0 /* ds is establishing connection */
6061
};

fs/nfs/pnfs_nfs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ print_ds(struct nfs4_pnfs_ds *ds)
338338
" client %p\n"
339339
" cl_exchange_flags %x\n",
340340
ds->ds_remotestr,
341-
atomic_read(&ds->ds_count), ds->ds_clp,
341+
refcount_read(&ds->ds_count), ds->ds_clp,
342342
ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
343343
}
344344

@@ -451,7 +451,7 @@ static void destroy_ds(struct nfs4_pnfs_ds *ds)
451451

452452
void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
453453
{
454-
if (atomic_dec_and_lock(&ds->ds_count,
454+
if (refcount_dec_and_lock(&ds->ds_count,
455455
&nfs4_ds_cache_lock)) {
456456
list_del_init(&ds->ds_node);
457457
spin_unlock(&nfs4_ds_cache_lock);
@@ -537,7 +537,7 @@ nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
537537
INIT_LIST_HEAD(&ds->ds_addrs);
538538
list_splice_init(dsaddrs, &ds->ds_addrs);
539539
ds->ds_remotestr = remotestr;
540-
atomic_set(&ds->ds_count, 1);
540+
refcount_set(&ds->ds_count, 1);
541541
INIT_LIST_HEAD(&ds->ds_node);
542542
ds->ds_clp = NULL;
543543
list_add(&ds->ds_node, &nfs4_data_server_cache);
@@ -546,10 +546,10 @@ nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
546546
} else {
547547
kfree(remotestr);
548548
kfree(ds);
549-
atomic_inc(&tmp_ds->ds_count);
549+
refcount_inc(&tmp_ds->ds_count);
550550
dprintk("%s data server %s found, inc'ed ds_count to %d\n",
551551
__func__, tmp_ds->ds_remotestr,
552-
atomic_read(&tmp_ds->ds_count));
552+
refcount_read(&tmp_ds->ds_count));
553553
ds = tmp_ds;
554554
}
555555
spin_unlock(&nfs4_ds_cache_lock);

0 commit comments

Comments
 (0)