Skip to content

Commit 81a090b

Browse files
ereshetovaamschuma-ntap
authored andcommitted
fs, nfs: convert nfs4_ff_layout_mirror.ref 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_ff_layout_mirror.ref 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 2b28a7b commit 81a090b

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

fs/nfs/flexfilelayout/flexfilelayout.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ ff_layout_add_mirror(struct pnfs_layout_hdr *lo,
187187
continue;
188188
if (!ff_mirror_match_fh(mirror, pos))
189189
continue;
190-
if (atomic_inc_not_zero(&pos->ref)) {
190+
if (refcount_inc_not_zero(&pos->ref)) {
191191
spin_unlock(&inode->i_lock);
192192
return pos;
193193
}
@@ -218,7 +218,7 @@ static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags)
218218
mirror = kzalloc(sizeof(*mirror), gfp_flags);
219219
if (mirror != NULL) {
220220
spin_lock_init(&mirror->lock);
221-
atomic_set(&mirror->ref, 1);
221+
refcount_set(&mirror->ref, 1);
222222
INIT_LIST_HEAD(&mirror->mirrors);
223223
}
224224
return mirror;
@@ -242,7 +242,7 @@ static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror)
242242

243243
static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror *mirror)
244244
{
245-
if (mirror != NULL && atomic_dec_and_test(&mirror->ref))
245+
if (mirror != NULL && refcount_dec_and_test(&mirror->ref))
246246
ff_layout_free_mirror(mirror);
247247
}
248248

@@ -2286,7 +2286,7 @@ ff_layout_mirror_prepare_stats(struct pnfs_layout_hdr *lo,
22862286
if (!test_and_clear_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags))
22872287
continue;
22882288
/* mirror refcount put in cleanup_layoutstats */
2289-
if (!atomic_inc_not_zero(&mirror->ref))
2289+
if (!refcount_inc_not_zero(&mirror->ref))
22902290
continue;
22912291
dev = &mirror->mirror_ds->id_node;
22922292
memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);

fs/nfs/flexfilelayout/flexfilelayout.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define FF_FLAGS_NO_IO_THRU_MDS 2
1414
#define FF_FLAGS_NO_READ_IO 4
1515

16+
#include <linux/refcount.h>
1617
#include "../pnfs.h"
1718

1819
/* XXX: Let's filter out insanely large mirror count for now to avoid oom
@@ -81,7 +82,7 @@ struct nfs4_ff_layout_mirror {
8182
nfs4_stateid stateid;
8283
struct rpc_cred __rcu *ro_cred;
8384
struct rpc_cred __rcu *rw_cred;
84-
atomic_t ref;
85+
refcount_t ref;
8586
spinlock_t lock;
8687
unsigned long flags;
8788
struct nfs4_ff_layoutstat read_stat;

0 commit comments

Comments
 (0)