Skip to content

Commit

Permalink
coda: convert from atomic_t to refcount_t on coda_vm_ops->refcnt
Browse files Browse the repository at this point in the history
refcount_t type and corresponding API can protect refcounters from
accidental underflow and overflow and further use-after-free situations.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Xiyu Yang <[email protected]>
Signed-off-by: Xin Tan <[email protected]>
Signed-off-by: Jan Harkes <[email protected]>
Cc: Alex Shi <[email protected]>
Cc: Jing Yangyang <[email protected]>
Cc: Zeal Robot <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
sherlly authored and torvalds committed Nov 9, 2021
1 parent 5a646fb commit 1077c28
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions fs/coda/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* to the Coda project. Contact Peter Braam <[email protected]>.
*/

#include <linux/refcount.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/time.h>
Expand All @@ -28,7 +29,7 @@
#include "coda_int.h"

struct coda_vm_ops {
atomic_t refcnt;
refcount_t refcnt;
struct file *coda_file;
const struct vm_operations_struct *host_vm_ops;
struct vm_operations_struct vm_ops;
Expand Down Expand Up @@ -98,7 +99,7 @@ coda_vm_open(struct vm_area_struct *vma)
struct coda_vm_ops *cvm_ops =
container_of(vma->vm_ops, struct coda_vm_ops, vm_ops);

atomic_inc(&cvm_ops->refcnt);
refcount_inc(&cvm_ops->refcnt);

if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->open)
cvm_ops->host_vm_ops->open(vma);
Expand All @@ -113,7 +114,7 @@ coda_vm_close(struct vm_area_struct *vma)
if (cvm_ops->host_vm_ops && cvm_ops->host_vm_ops->close)
cvm_ops->host_vm_ops->close(vma);

if (atomic_dec_and_test(&cvm_ops->refcnt)) {
if (refcount_dec_and_test(&cvm_ops->refcnt)) {
vma->vm_ops = cvm_ops->host_vm_ops;
fput(cvm_ops->coda_file);
kfree(cvm_ops);
Expand Down Expand Up @@ -189,7 +190,7 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
cvm_ops->vm_ops.open = coda_vm_open;
cvm_ops->vm_ops.close = coda_vm_close;
cvm_ops->coda_file = coda_file;
atomic_set(&cvm_ops->refcnt, 1);
refcount_set(&cvm_ops->refcnt, 1);

vma->vm_ops = &cvm_ops->vm_ops;
}
Expand Down

0 comments on commit 1077c28

Please sign in to comment.