Commit d4f0284a authored by Elena Reshetova's avatar Elena Reshetova Committed by Linus Torvalds

fs, nilfs: convert nilfs_root.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 nilfs_root.count is used as pure reference counter.
Convert it to refcount_t and fix up the operations.

Link: http://lkml.kernel.org/r/1509367935-3086-3-git-send-email-konishi.ryusuke@lab.ntt.co.jpSigned-off-by: default avatarElena Reshetova <elena.reshetova@intel.com>
Signed-off-by: default avatarRyusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
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 avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 31ccb1f7
...@@ -737,7 +737,7 @@ struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno) ...@@ -737,7 +737,7 @@ struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
} else if (cno > root->cno) { } else if (cno > root->cno) {
n = n->rb_right; n = n->rb_right;
} else { } else {
atomic_inc(&root->count); refcount_inc(&root->count);
spin_unlock(&nilfs->ns_cptree_lock); spin_unlock(&nilfs->ns_cptree_lock);
return root; return root;
} }
...@@ -776,7 +776,7 @@ nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno) ...@@ -776,7 +776,7 @@ nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
} else if (cno > root->cno) { } else if (cno > root->cno) {
p = &(*p)->rb_right; p = &(*p)->rb_right;
} else { } else {
atomic_inc(&root->count); refcount_inc(&root->count);
spin_unlock(&nilfs->ns_cptree_lock); spin_unlock(&nilfs->ns_cptree_lock);
kfree(new); kfree(new);
return root; return root;
...@@ -786,7 +786,7 @@ nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno) ...@@ -786,7 +786,7 @@ nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
new->cno = cno; new->cno = cno;
new->ifile = NULL; new->ifile = NULL;
new->nilfs = nilfs; new->nilfs = nilfs;
atomic_set(&new->count, 1); refcount_set(&new->count, 1);
atomic64_set(&new->inodes_count, 0); atomic64_set(&new->inodes_count, 0);
atomic64_set(&new->blocks_count, 0); atomic64_set(&new->blocks_count, 0);
...@@ -806,7 +806,7 @@ nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno) ...@@ -806,7 +806,7 @@ nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
void nilfs_put_root(struct nilfs_root *root) void nilfs_put_root(struct nilfs_root *root)
{ {
if (atomic_dec_and_test(&root->count)) { if (refcount_dec_and_test(&root->count)) {
struct the_nilfs *nilfs = root->nilfs; struct the_nilfs *nilfs = root->nilfs;
nilfs_sysfs_delete_snapshot_group(root); nilfs_sysfs_delete_snapshot_group(root);
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <linux/blkdev.h> #include <linux/blkdev.h>
#include <linux/backing-dev.h> #include <linux/backing-dev.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/refcount.h>
struct nilfs_sc_info; struct nilfs_sc_info;
struct nilfs_sysfs_dev_subgroups; struct nilfs_sysfs_dev_subgroups;
...@@ -246,7 +247,7 @@ struct nilfs_root { ...@@ -246,7 +247,7 @@ struct nilfs_root {
__u64 cno; __u64 cno;
struct rb_node rb_node; struct rb_node rb_node;
atomic_t count; refcount_t count;
struct the_nilfs *nilfs; struct the_nilfs *nilfs;
struct inode *ifile; struct inode *ifile;
...@@ -299,7 +300,7 @@ void nilfs_swap_super_block(struct the_nilfs *); ...@@ -299,7 +300,7 @@ void nilfs_swap_super_block(struct the_nilfs *);
static inline void nilfs_get_root(struct nilfs_root *root) static inline void nilfs_get_root(struct nilfs_root *root)
{ {
atomic_inc(&root->count); refcount_inc(&root->count);
} }
static inline int nilfs_valid_fs(struct the_nilfs *nilfs) static inline int nilfs_valid_fs(struct the_nilfs *nilfs)
......
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