Skip to content

Commit

Permalink
Merge pull request #266 from Ayxan13/fix-sleep-toctou
Browse files Browse the repository at this point in the history
Fix blocking O_NONBLOCK process bug
  • Loading branch information
jserv authored Aug 12, 2024
2 parents 84cc7fe + 4adbf1d commit 47663d6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions examples/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,18 @@ static DECLARE_WAIT_QUEUE_HEAD(waitq);
/* Called when the /proc file is opened */
static int module_open(struct inode *inode, struct file *file)
{
/* Try to get without blocking */
if (!atomic_cmpxchg(&already_open, 0, 1)) {
/* Success without blocking, allow the access */
try_module_get(THIS_MODULE);
return 0;
}
/* If the file's flags include O_NONBLOCK, it means the process does not
* want to wait for the file. In this case, if the file is already open,
* want to wait for the file. In this case, because the file is already open,
* we should fail with -EAGAIN, meaning "you will have to try again",
* instead of blocking a process which would rather stay awake.
*/
if ((file->f_flags & O_NONBLOCK) && atomic_read(&already_open))
if (file->f_flags & O_NONBLOCK)
return -EAGAIN;

/* This is the correct place for try_module_get(THIS_MODULE) because if
Expand Down

0 comments on commit 47663d6

Please sign in to comment.