Skip to content

Commit 2b28a7b

Browse files
ereshetovaamschuma-ntap
authored andcommitted
fs, nfs: convert pnfs_layout_hdr.plh_refcount 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 pnfs_layout_hdr.plh_refcount 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 eba6dd6 commit 2b28a7b

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

fs/nfs/pnfs.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
251251
void
252252
pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
253253
{
254-
atomic_inc(&lo->plh_refcount);
254+
refcount_inc(&lo->plh_refcount);
255255
}
256256

257257
static struct pnfs_layout_hdr *
@@ -296,7 +296,7 @@ pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
296296

297297
pnfs_layoutreturn_before_put_layout_hdr(lo);
298298

299-
if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
299+
if (refcount_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
300300
if (!list_empty(&lo->plh_segs))
301301
WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
302302
pnfs_detach_layout_hdr(lo);
@@ -395,14 +395,14 @@ pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
395395
{
396396
lo->plh_retry_timestamp = jiffies;
397397
if (!test_and_set_bit(fail_bit, &lo->plh_flags))
398-
atomic_inc(&lo->plh_refcount);
398+
refcount_inc(&lo->plh_refcount);
399399
}
400400

401401
static void
402402
pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
403403
{
404404
if (test_and_clear_bit(fail_bit, &lo->plh_flags))
405-
atomic_dec(&lo->plh_refcount);
405+
refcount_dec(&lo->plh_refcount);
406406
}
407407

408408
static void
@@ -472,7 +472,7 @@ pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
472472
WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
473473
list_del_init(&lseg->pls_list);
474474
/* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
475-
atomic_dec(&lo->plh_refcount);
475+
refcount_dec(&lo->plh_refcount);
476476
if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
477477
return;
478478
if (list_empty(&lo->plh_segs) &&
@@ -1451,7 +1451,7 @@ alloc_init_layout_hdr(struct inode *ino,
14511451
lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
14521452
if (!lo)
14531453
return NULL;
1454-
atomic_set(&lo->plh_refcount, 1);
1454+
refcount_set(&lo->plh_refcount, 1);
14551455
INIT_LIST_HEAD(&lo->plh_layouts);
14561456
INIT_LIST_HEAD(&lo->plh_segs);
14571457
INIT_LIST_HEAD(&lo->plh_return_segs);

fs/nfs/pnfs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ struct pnfs_layoutdriver_type {
180180
};
181181

182182
struct pnfs_layout_hdr {
183-
atomic_t plh_refcount;
183+
refcount_t plh_refcount;
184184
atomic_t plh_outstanding; /* number of RPCs out */
185185
struct list_head plh_layouts; /* other client layouts */
186186
struct list_head plh_bulk_destroy;

0 commit comments

Comments
 (0)