Skip to content

Commit 0e7d523

Browse files
Al Virogregkh
authored andcommitted
ntsync: fix a file reference leak in drivers/misc/ntsync.c
struct ntsync_obj contains a reference to struct file and that reference contributes to refcount - ntsync_alloc_obj() grabs it. Normally the object is destroyed (and reference to obj->file dropped) in ntsync_obj_release(). However, in case of ntsync_obj_get_fd() failure the object is destroyed directly by its creator. That case should also drop obj->file; plain kfree(obj) is not enough there - it ends up leaking struct file * reference. Take that logics into a helper (ntsync_free_obj()) and use it in both codepaths that destroy ntsync_obj instances. Fixes: b46271e "ntsync: Introduce NTSYNC_IOC_CREATE_SEM" Signed-off-by: Al Viro <[email protected]> Reviewed-by: Elizabeth Figura <[email protected]> Link: https://lore.kernel.org/r/20250115025002.GA1977892@ZenIV Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 2217573 commit 0e7d523

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/misc/ntsync.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,13 +651,15 @@ static int ntsync_event_read(struct ntsync_obj *event, void __user *argp)
651651
return 0;
652652
}
653653

654-
static int ntsync_obj_release(struct inode *inode, struct file *file)
654+
static void ntsync_free_obj(struct ntsync_obj *obj)
655655
{
656-
struct ntsync_obj *obj = file->private_data;
657-
658656
fput(obj->dev->file);
659657
kfree(obj);
658+
}
660659

660+
static int ntsync_obj_release(struct inode *inode, struct file *file)
661+
{
662+
ntsync_free_obj(file->private_data);
661663
return 0;
662664
}
663665

@@ -755,7 +757,7 @@ static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp)
755757
sem->u.sem.max = args.max;
756758
fd = ntsync_obj_get_fd(sem);
757759
if (fd < 0)
758-
kfree(sem);
760+
ntsync_free_obj(sem);
759761

760762
return fd;
761763
}

0 commit comments

Comments
 (0)