Skip to content

Commit

Permalink
audit: convert audit_watch.count from atomic_t to refcount_t
Browse files Browse the repository at this point in the history
refcount_t type and corresponding API should be
used instead of atomic_t when the variable is used as
a reference counter. This allows to avoid accidental
refcounter overflows that might lead to use-after-free
situations.

Signed-off-by: Elena Reshetova <[email protected]>
Signed-off-by: Hans Liljestrand <[email protected]>
Signed-off-by: Kees Cook <[email protected]>
Signed-off-by: David Windsor <[email protected]>
[PM: fix subject line, add #include]
Signed-off-by: Paul Moore <[email protected]>
  • Loading branch information
ereshetova authored and pcmoore committed May 2, 2017
1 parent 9d2378f commit bd120de
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions kernel/audit_watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <linux/fsnotify_backend.h>
#include <linux/namei.h>
#include <linux/netlink.h>
#include <linux/refcount.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/security.h>
Expand All @@ -46,7 +47,7 @@
*/

struct audit_watch {
atomic_t count; /* reference count */
refcount_t count; /* reference count */
dev_t dev; /* associated superblock device */
char *path; /* insertion path */
unsigned long ino; /* associated inode number */
Expand Down Expand Up @@ -111,12 +112,12 @@ static inline struct audit_parent *audit_find_parent(struct inode *inode)

void audit_get_watch(struct audit_watch *watch)
{
atomic_inc(&watch->count);
refcount_inc(&watch->count);
}

void audit_put_watch(struct audit_watch *watch)
{
if (atomic_dec_and_test(&watch->count)) {
if (refcount_dec_and_test(&watch->count)) {
WARN_ON(watch->parent);
WARN_ON(!list_empty(&watch->rules));
kfree(watch->path);
Expand Down Expand Up @@ -178,7 +179,7 @@ static struct audit_watch *audit_init_watch(char *path)
return ERR_PTR(-ENOMEM);

INIT_LIST_HEAD(&watch->rules);
atomic_set(&watch->count, 1);
refcount_set(&watch->count, 1);
watch->path = path;
watch->dev = AUDIT_DEV_UNSET;
watch->ino = AUDIT_INO_UNSET;
Expand Down

0 comments on commit bd120de

Please sign in to comment.