lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20170131020638.hf2pzz55u4tymvot@dhcp-1-212.brq.redhat.com>
Date:   Tue, 31 Jan 2017 03:06:39 +0100
From:   Mateusz Guzik <mguzik@...hat.com>
To:     Dmitry Vyukov <dvyukov@...gle.com>
Cc:     Al Viro <viro@...iv.linux.org.uk>,
        Thomas Gleixner <tglx@...utronix.de>,
        "linux-fsdevel@...r.kernel.org" <linux-fsdevel@...r.kernel.org>,
        LKML <linux-kernel@...r.kernel.org>,
        syzkaller <syzkaller@...glegroups.com>
Subject: Re: timerfd: use-after-free in timerfd_remove_cancel

On Mon, Jan 30, 2017 at 07:41:59PM +0100, Dmitry Vyukov wrote:
> Hello,
> 
> The following program triggers use-after-free in timerfd_remove_cancel:
> https://gist.githubusercontent.com/dvyukov/202576d437c84ffbbe52e9ccd77e1b44/raw/5562bff8626a73627157331ea2b837f59080ac84/gistfile1.txt
> 
> BUG: KASAN: use-after-free in __list_del include/linux/list.h:104
> [inline] at addr ffff88006bab1410
> BUG: KASAN: use-after-free in __list_del_entry
> include/linux/list.h:119 [inline] at addr ffff88006bab1410
> BUG: KASAN: use-after-free in list_del_rcu include/linux/rculist.h:129
> [inline] at addr ffff88006bab1410
> BUG: KASAN: use-after-free in timerfd_remove_cancel fs/timerfd.c:120
> [inline] at addr ffff88006bab1410
> BUG: KASAN: use-after-free in timerfd_release+0x28e/0x310
> fs/timerfd.c:209 at addr ffff88006bab1410
> Write of size 8 by task a.out/2897
> 
[..]
> Seems that ctx->might_cancel is racy.
> 

Indeed it is. Can you try the patch below please. If it works I'll send
it in a nicer form.

diff --git a/fs/timerfd.c b/fs/timerfd.c
index c173cc1..63f91c3 100644
--- a/fs/timerfd.c
+++ b/fs/timerfd.c
@@ -112,14 +112,30 @@ void timerfd_clock_was_set(void)
 	rcu_read_unlock();
 }
 
+static void timerfd_set_cancel(struct timerfd_ctx *ctx)
+{
+	if (ctx->might_cancel)
+		return;
+
+	spin_lock(&cancel_lock);
+	if (!ctx->might_cancel) {
+		ctx->might_cancel = true;
+		list_add_rcu(&ctx->clist, &cancel_list);
+	}
+	spin_unlock(&cancel_lock);
+}
+
 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
 {
+	if (!ctx->might_cancel)
+		return;
+
+	spin_lock(&cancel_lock);
 	if (ctx->might_cancel) {
 		ctx->might_cancel = false;
-		spin_lock(&cancel_lock);
 		list_del_rcu(&ctx->clist);
-		spin_unlock(&cancel_lock);
 	}
+	spin_unlock(&cancel_lock);
 }
 
 static bool timerfd_canceled(struct timerfd_ctx *ctx)
@@ -134,16 +150,10 @@ static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
 {
 	if ((ctx->clockid == CLOCK_REALTIME ||
 	     ctx->clockid == CLOCK_REALTIME_ALARM) &&
-	    (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
-		if (!ctx->might_cancel) {
-			ctx->might_cancel = true;
-			spin_lock(&cancel_lock);
-			list_add_rcu(&ctx->clist, &cancel_list);
-			spin_unlock(&cancel_lock);
-		}
-	} else if (ctx->might_cancel) {
+	    (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET))
+		timerfd_set_cancel(ctx);
+	else
 		timerfd_remove_cancel(ctx);
-	}
 }
 
 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ