Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1580516pxb; Mon, 22 Feb 2021 05:55:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJy/LM6jNZkA0kJdLpvgwdjj0ck0QFyqPLoyugUVoOL3d135gb8NooKH+kiGjUQKj+nPfv/t X-Received: by 2002:aa7:d38b:: with SMTP id x11mr22486123edq.23.1614002148891; Mon, 22 Feb 2021 05:55:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614002148; cv=none; d=google.com; s=arc-20160816; b=G+TWnfaZLEcbI6acmVOLivsxsyqyEbaWyydkzQR/oWiRpW7vZfauEozKmcLfmnOwLv /8lcUb+qK1qvLlMUtknXbmeOntw7ouJ39cQwIHEB8MjoILNvpQpwzKXD1V/S6o5xO+6v 2vQ/wVyPRKkLE+jRTWA3Rr6Ivzf1oAukz/dhdaxJHQlEt5U38u7isoBQbJRs8qpARajW bbzvDlO6W5YIktJ/2dKgECZrlDvKGpDCTFz9BEEyQHa5adXHKs4/j50Xr7Q9l1a/Sm+H kemY7O0C5yarZYGsYut1Q5/66SqsIzJ+WXiwekD37TuBR0G66guCwxxTng3L4vRZ3cAY DeLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=n3qD7rn0LZ4IbpxqW9aeoMZVB/Vr23TXYRL+4hB4HpI=; b=PEm8A/Ckx+VF1nlAJvMlTZhbnfIy6r3rQSY6Fw+i47COjxzTzUW/MXdKc3caGBM/NK S70VpjOl0yTQ9u0CSwDABLpVD9ZhtuWWw7RzazGHgFZBbepUM5eI8CJ4+8DTgY3YBmbY qg5E46Fs89SNrCYAz8Y3fy+oBI0XER0+KlZ7JL3jG1wiJbQgFPqH8MsJn3Oo3v5Pm5Sh olER7evqjG/0DCyzpBaggQk4/N9yUUv7U6UbXb6xUtYUqnOBc22Kx/ucU9XXsshVIPpF F7uYIJTWWPRFe9uR5BBwlqC8GywrYzZPCxugX+eQKIkwrAuIz4ddPkMryfT6gs9H5KEI +Mkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MEKqEQ+F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mb25si12623627ejb.519.2021.02.22.05.55.26; Mon, 22 Feb 2021 05:55:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MEKqEQ+F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231638AbhBVNyv (ORCPT + 99 others); Mon, 22 Feb 2021 08:54:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:56550 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231592AbhBVMq7 (ORCPT ); Mon, 22 Feb 2021 07:46:59 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5A03964F1C; Mon, 22 Feb 2021 12:42:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1613997729; bh=v4mVMlTseaZnhl6T1nV56LIY6+wTCLEyIChR00NhFeM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MEKqEQ+Fe/Bh7VmFuGoLpXYT4GsgE4G0FX11LFw0iAFiG8BnbwEfYKZRtvO6LE3+i 1X+u35+009X3zw6Q7C+9VEQL3+fILwhcQIzSgJ7UXeLhZNi5Jrn8jkdWkZb0xvnGp2 8ALi5hZ71LTe6Jt/+Y7uFROBBzo2NcmS5LvN8U0Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Stefan Liebler , Thomas Gleixner , Peter Zijlstra , Heiko Carstens , Darren Hart , Ingo Molnar , Sasha Levin , Sudip Mukherjee , Lee Jones Subject: [PATCH 4.9 15/49] futex: Cure exit race Date: Mon, 22 Feb 2021 13:36:13 +0100 Message-Id: <20210222121025.999623772@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210222121022.546148341@linuxfoundation.org> References: <20210222121022.546148341@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Gleixner commit da791a667536bf8322042e38ca85d55a78d3c273 upstream. Stefan reported, that the glibc tst-robustpi4 test case fails occasionally. That case creates the following race between sys_exit() and sys_futex_lock_pi(): CPU0 CPU1 sys_exit() sys_futex() do_exit() futex_lock_pi() exit_signals(tsk) No waiters: tsk->flags |= PF_EXITING; *uaddr == 0x00000PID mm_release(tsk) Set waiter bit exit_robust_list(tsk) { *uaddr = 0x80000PID; Set owner died attach_to_pi_owner() { *uaddr = 0xC0000000; tsk = get_task(PID); } if (!tsk->flags & PF_EXITING) { ... attach(); tsk->flags |= PF_EXITPIDONE; } else { if (!(tsk->flags & PF_EXITPIDONE)) return -EAGAIN; return -ESRCH; <--- FAIL } ESRCH is returned all the way to user space, which triggers the glibc test case assert. Returning ESRCH unconditionally is wrong here because the user space value has been changed by the exiting task to 0xC0000000, i.e. the FUTEX_OWNER_DIED bit is set and the futex PID value has been cleared. This is a valid state and the kernel has to handle it, i.e. taking the futex. Cure it by rereading the user space value when PF_EXITING and PF_EXITPIDONE is set in the task which 'owns' the futex. If the value has changed, let the kernel retry the operation, which includes all regular sanity checks and correctly handles the FUTEX_OWNER_DIED case. If it hasn't changed, then return ESRCH as there is no way to distinguish this case from malfunctioning user space. This happens when the exiting task did not have a robust list, the robust list was corrupted or the user space value in the futex was simply bogus. Reported-by: Stefan Liebler Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra Cc: Heiko Carstens Cc: Darren Hart Cc: Ingo Molnar Cc: Sasha Levin Cc: stable@vger.kernel.org Link: https://bugzilla.kernel.org/show_bug.cgi?id=200467 Link: https://lkml.kernel.org/r/20181210152311.986181245@linutronix.de Signed-off-by: Sudip Mukherjee Signed-off-by: Greg Kroah-Hartman [Lee: Required to satisfy functional dependency from futex back-port. Re-add the missing handle_exit_race() parts from: 3d4775df0a89 ("futex: Replace PF_EXITPIDONE with a state")] Signed-off-by: Lee Jones Signed-off-by: Greg Kroah-Hartman --- kernel/futex.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 6 deletions(-) --- a/kernel/futex.c +++ b/kernel/futex.c @@ -1201,11 +1201,67 @@ static void wait_for_owner_exiting(int r put_task_struct(exiting); } +static int handle_exit_race(u32 __user *uaddr, u32 uval, + struct task_struct *tsk) +{ + u32 uval2; + + /* + * If the futex exit state is not yet FUTEX_STATE_DEAD, wait + * for it to finish. + */ + if (tsk && tsk->futex_state != FUTEX_STATE_DEAD) + return -EAGAIN; + + /* + * Reread the user space value to handle the following situation: + * + * CPU0 CPU1 + * + * sys_exit() sys_futex() + * do_exit() futex_lock_pi() + * futex_lock_pi_atomic() + * exit_signals(tsk) No waiters: + * tsk->flags |= PF_EXITING; *uaddr == 0x00000PID + * mm_release(tsk) Set waiter bit + * exit_robust_list(tsk) { *uaddr = 0x80000PID; + * Set owner died attach_to_pi_owner() { + * *uaddr = 0xC0000000; tsk = get_task(PID); + * } if (!tsk->flags & PF_EXITING) { + * ... attach(); + * tsk->futex_state = } else { + * FUTEX_STATE_DEAD; if (tsk->futex_state != + * FUTEX_STATE_DEAD) + * return -EAGAIN; + * return -ESRCH; <--- FAIL + * } + * + * Returning ESRCH unconditionally is wrong here because the + * user space value has been changed by the exiting task. + * + * The same logic applies to the case where the exiting task is + * already gone. + */ + if (get_futex_value_locked(&uval2, uaddr)) + return -EFAULT; + + /* If the user space value has changed, try again. */ + if (uval2 != uval) + return -EAGAIN; + + /* + * The exiting task did not have a robust list, the robust list was + * corrupted or the user space value in *uaddr is simply bogus. + * Give up and tell user space. + */ + return -ESRCH; +} + /* * Lookup the task for the TID provided from user space and attach to * it after doing proper sanity checks. */ -static int attach_to_pi_owner(u32 uval, union futex_key *key, +static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key, struct futex_pi_state **ps, struct task_struct **exiting) { @@ -1216,12 +1272,15 @@ static int attach_to_pi_owner(u32 uval, /* * We are the first waiter - try to look up the real owner and attach * the new pi_state to it, but bail out when TID = 0 [1] + * + * The !pid check is paranoid. None of the call sites should end up + * with pid == 0, but better safe than sorry. Let the caller retry */ if (!pid) - return -ESRCH; + return -EAGAIN; p = futex_find_get_task(pid); if (!p) - return -ESRCH; + return handle_exit_race(uaddr, uval, NULL); if (unlikely(p->flags & PF_KTHREAD)) { put_task_struct(p); @@ -1240,7 +1299,7 @@ static int attach_to_pi_owner(u32 uval, * FUTEX_STATE_DEAD, we know that the task has finished * the cleanup: */ - int ret = (p->futex_state = FUTEX_STATE_DEAD) ? -ESRCH : -EAGAIN; + int ret = handle_exit_race(uaddr, uval, p); raw_spin_unlock_irq(&p->pi_lock); /* @@ -1306,7 +1365,7 @@ static int lookup_pi_state(u32 __user *u * We are the first waiter - try to look up the owner based on * @uval and attach to it. */ - return attach_to_pi_owner(uval, key, ps, exiting); + return attach_to_pi_owner(uaddr, uval, key, ps, exiting); } static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval) @@ -1422,7 +1481,7 @@ static int futex_lock_pi_atomic(u32 __us * attach to the owner. If that fails, no harm done, we only * set the FUTEX_WAITERS bit in the user space variable. */ - return attach_to_pi_owner(uval, key, ps, exiting); + return attach_to_pi_owner(uaddr, newval, key, ps, exiting); } /**