Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4564834imu; Tue, 29 Jan 2019 03:54:34 -0800 (PST) X-Google-Smtp-Source: ALg8bN6cNFGcGq9A1I4GZQ5h7JOkZT/XSHX6jn16xnYRqjTU5Zn4rDhcv4FNsQpkNZw+EiLZ+le1 X-Received: by 2002:a17:902:8346:: with SMTP id z6mr25650019pln.340.1548762874679; Tue, 29 Jan 2019 03:54:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548762874; cv=none; d=google.com; s=arc-20160816; b=ztN5gnYEx/04uvKMhAt8WlaYX1D5zVvHTLyNLFE88Z136R6KiHmXX3HB45XLZ+beiC Sn5zT837/wqykSJVopIba3XZ6MLknWO75qFUpdGUfdQ3FDaWN0GcRXJvY9ifmMK/khdT 1aOihm3WsaNK/YRtAf/VXVzKe+MiAy/uCdtDD+TVAgJJN3dZY1LW6NUCIxOXhZ+j/NFe 6X126NdXN27ORlylMNBotxpMREoV0+sOmAiRJnx2Z8iR4Sejxifb6OMOKu1Kc+bMLqyk pSafFrRpuRZGNC1+kgI12uCY+YzjNDEKhYaUlFHuLjlXAkNuoD5l1FxcM4RIE/U1avJn Bf/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=165Ac/ZeoqlU3zWWJ17UhWRVjBlqXu6kAJmTkClE32o=; b=QEm/3U0eIxwgSI3Lg7Wxd2/e7FH/vzmsZ0neH54btIbMNbZv72UVXxdYoN1HrMHV32 yvadPy8oswsVJiVwgX0coqvBVMyNGQKIxHU6DepoeRyaF8FNcZlu2mTq9oQKEgwyu0+T VZsD+baZWhffI3HaOENpc01Q+7+ltRiR/KflUKcXbj7wQ4SwiZbBDvxJuJ7+ZIWOkBcp taCXPXgx3bmQJFCKPNcoz4vHlDHF4T+yJn4xsNfSVytuIPBNOxQu7vDg4N4vAsF7smDD MZk0JbiNJRrEWNbHkSx8TLGpV40TbcXsE69KRkUr8taI3iVrkOt2MIzPb+a2V86+o0pA pUxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oN5o6Cen; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z18si35548199plo.89.2019.01.29.03.54.19; Tue, 29 Jan 2019 03:54:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oN5o6Cen; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731943AbfA2Lvz (ORCPT + 99 others); Tue, 29 Jan 2019 06:51:55 -0500 Received: from mail.kernel.org ([198.145.29.99]:43402 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731966AbfA2Lvs (ORCPT ); Tue, 29 Jan 2019 06:51:48 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 63FC120989; Tue, 29 Jan 2019 11:51:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548762707; bh=UnCwPL2AWeeVzXLoc1/k94Q4XwKehXo62htDZm7dt5w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oN5o6CenfkCuhhPtEUnwnUuOB2lLTk7YRtaH1vUp9EjjwRIG3bi0HrOP323QNRwyO bfN8DqxMwzck6DmM4Nx8dtdWViVhYXPpXj1QSTrEgsjmPUVTXRSp7yL4uvZs2pYgDj kPMxo724SG5eGQG2RHnuyuy9kNL1yP5knzrymCAY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Hansen , Thomas Gleixner , bp@alien8.de, hpa@zytor.com, peterz@infradead.org, mpe@ellerman.id.au, will.deacon@arm.com, luto@kernel.org, jroedel@suse.de Subject: [PATCH 4.9 31/44] x86/selftests/pkeys: Fork() to check for state being preserved Date: Tue, 29 Jan 2019 12:36:26 +0100 Message-Id: <20190129113142.435160949@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190129113139.826927690@linuxfoundation.org> References: <20190129113139.826927690@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dave Hansen commit e1812933b17be7814f51b6c310c5d1ced7a9a5f5 upstream. There was a bug where the per-mm pkey state was not being preserved across fork() in the child. fork() is performed in the pkey selftests, but all of the pkey activity is performed in the parent. The child does not perform any actions sensitive to pkey state. To make the test more sensitive to these kinds of bugs, add a fork() where the parent exits, and execution continues in the child. To achieve this let the key exhaustion test not terminate at the first allocation failure and fork after 2*NR_PKEYS loops and continue in the child. Signed-off-by: Dave Hansen Signed-off-by: Thomas Gleixner Cc: bp@alien8.de Cc: hpa@zytor.com Cc: peterz@infradead.org Cc: mpe@ellerman.id.au Cc: will.deacon@arm.com Cc: luto@kernel.org Cc: jroedel@suse.de Cc: stable@vger.kernel.org Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: Peter Zijlstra Cc: Michael Ellerman Cc: Will Deacon Cc: Andy Lutomirski Cc: Joerg Roedel Link: https://lkml.kernel.org/r/20190102215657.585704B7@viggo.jf.intel.com Signed-off-by: Greg Kroah-Hartman --- tools/testing/selftests/x86/protection_keys.c | 41 +++++++++++++++++++------- 1 file changed, 31 insertions(+), 10 deletions(-) --- a/tools/testing/selftests/x86/protection_keys.c +++ b/tools/testing/selftests/x86/protection_keys.c @@ -1129,6 +1129,21 @@ void test_pkey_syscalls_bad_args(int *pt pkey_assert(err); } +void become_child(void) +{ + pid_t forkret; + + forkret = fork(); + pkey_assert(forkret >= 0); + dprintf3("[%d] fork() ret: %d\n", getpid(), forkret); + + if (!forkret) { + /* in the child */ + return; + } + exit(0); +} + /* Assumes that all pkeys other than 'pkey' are unallocated */ void test_pkey_alloc_exhaust(int *ptr, u16 pkey) { @@ -1139,7 +1154,7 @@ void test_pkey_alloc_exhaust(int *ptr, u int nr_allocated_pkeys = 0; int i; - for (i = 0; i < NR_PKEYS*2; i++) { + for (i = 0; i < NR_PKEYS*3; i++) { int new_pkey; dprintf1("%s() alloc loop: %d\n", __func__, i); new_pkey = alloc_pkey(); @@ -1150,21 +1165,27 @@ void test_pkey_alloc_exhaust(int *ptr, u if ((new_pkey == -1) && (errno == ENOSPC)) { dprintf2("%s() failed to allocate pkey after %d tries\n", __func__, nr_allocated_pkeys); - break; + } else { + /* + * Ensure the number of successes never + * exceeds the number of keys supported + * in the hardware. + */ + pkey_assert(nr_allocated_pkeys < NR_PKEYS); + allocated_pkeys[nr_allocated_pkeys++] = new_pkey; } - pkey_assert(nr_allocated_pkeys < NR_PKEYS); - allocated_pkeys[nr_allocated_pkeys++] = new_pkey; + + /* + * Make sure that allocation state is properly + * preserved across fork(). + */ + if (i == NR_PKEYS*2) + become_child(); } dprintf3("%s()::%d\n", __func__, __LINE__); /* - * ensure it did not reach the end of the loop without - * failure: - */ - pkey_assert(i < NR_PKEYS*2); - - /* * There are 16 pkeys supported in hardware. One is taken * up for the default (0) and another can be taken up by * an execute-only mapping. Ensure that we can allocate