From patchwork Fri Mar 20 20:24:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 11450265 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B84B21668 for ; Fri, 20 Mar 2020 20:24:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 85C0D20739 for ; Fri, 20 Mar 2020 20:24:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 85C0D20739 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=hotmail.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C4A7E6B000A; Fri, 20 Mar 2020 16:24:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C057B6B000C; Fri, 20 Mar 2020 16:24:57 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE9546B000D; Fri, 20 Mar 2020 16:24:57 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0199.hostedemail.com [216.40.44.199]) by kanga.kvack.org (Postfix) with ESMTP id 9336E6B000A for ; Fri, 20 Mar 2020 16:24:57 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 470836D63 for ; Fri, 20 Mar 2020 20:24:57 +0000 (UTC) X-FDA: 76616869434.10.lock93_82740c94b164e X-Spam-Summary: 2,0,0,c51942882aab081a,d41d8cd98f00b204,bernd.edlinger@hotmail.de,,RULES_HIT:41:69:355:379:800:854:960:973:988:989:1260:1261:1277:1311:1313:1314:1345:1359:1381:1437:1515:1516:1518:1535:1542:1593:1594:1683:1711:1730:1747:1777:1792:2393:2553:2559:2562:2895:3138:3139:3140:3141:3142:3353:3865:3867:3868:3870:3871:3872:3874:4120:5007:6691:6737:6738:7652:9592:10004:10400:11026:11232:11658:11914:12043:12048:12160:12291:12296:12297:12438:12519:12555:12683:12760:12895:13255:14096:14097:14181:14394:14721:21080:21212:21324:21444:21451:21627:21990:30012:30054:30055:30070:30090,0,RBL:40.92.74.103:@hotmail.de:.lbl8.mailshell.net-62.18.114.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: lock93_82740c94b164e X-Filterd-Recvd-Size: 9115 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-oln040092074103.outbound.protection.outlook.com [40.92.74.103]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Mar 2020 20:24:56 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JfeQxRmZ9RJWqUeysvOl/6WOUFFcb6jSqhbZsgPp3D99zTDkrjIxWXvYQvsH2pHKrhIgoW5VjsNFAc2yXH3qirOxNEvkih1+lakEL2M50ULtDEtP5BKrOdkLoB1Z0M+jD5robXXI4zSnKp78Tzwr98FlFY/bWi7cs/FONPVmSf9mSIMaE6HzqWytsYuyM0PBc+p3Aup57RFfLdFMP8ivp804AHDviwmklBVmFJRz/NZ3DcEXeQbWNxy6wkHR1aapmVj34oJFeJlwsTEvuDOA8UxdEMIvdT4hFaHUa1DU49hdvndF2LyXZkuxNJqfCrhUqUMRPQETffzMiBICbmsPOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iEG1G63BQyTj4NIAgyL0sILHWEZk/yOpP/x1R6xqgjk=; b=iTwBx6xkJ9vuvcMuECHEEl4w3jrBP1MJCnmzssu/MOsyj2MzpitBi6zNVDrEg/xlgH1ZZvcDr9/3fDZ+ELePUR0Y/WJ5j0eGRm6C7D1NcfKJ6YpfyO9xfB1yrBdLl4xw/kwuqnGQPF4KiqAwQjeO3vd8Mc23hengjs3mpHiQooHc0Kyy7Xdqwamc60S/FQXs1WKPC2iFUVE6ItLUfxbRWQDT4V5riaDwp3g8F4usnlwaTWQXD/9Fp5fyYbLjyWRmRRmaf8QTYtCTNYWWtaPnlRQrGltC4L0GWM3fsu0h3i/q11z4axajpIRjcigS899vPjvX/O8Obs5KstGTw4hvcQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=hotmail.de; dmarc=pass action=none header.from=hotmail.de; dkim=pass header.d=hotmail.de; arc=none Received: from DB3EUR04FT027.eop-eur04.prod.protection.outlook.com (2a01:111:e400:7e0c::36) by DB3EUR04HT021.eop-eur04.prod.protection.outlook.com (2a01:111:e400:7e0c::446) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2814.13; Fri, 20 Mar 2020 20:24:54 +0000 Received: from AM6PR03MB5170.eurprd03.prod.outlook.com (10.152.24.52) by DB3EUR04FT027.mail.protection.outlook.com (10.152.24.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2814.13 via Frontend Transport; Fri, 20 Mar 2020 20:24:54 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:869908CF01EFF52FDCDECC38EB77254CAEFC249ED423CE489D332E457C4D9BB9;UpperCasedChecksum:5371DEE21799D84893EF9CDD123E730B7C6AD75BD50B949988FB24A3540839E0;SizeAsReceived:9432;Count:49 Received: from AM6PR03MB5170.eurprd03.prod.outlook.com ([fe80::1956:d274:cab3:b4dd]) by AM6PR03MB5170.eurprd03.prod.outlook.com ([fe80::1956:d274:cab3:b4dd%6]) with mapi id 15.20.2835.017; Fri, 20 Mar 2020 20:24:54 +0000 From: Bernd Edlinger Subject: [PATCH v6 02/16] exec: Factor unshare_sighand out of de_thread and call it separately To: Greg Kroah-Hartman , Kirill Tkhai , "Eric W. Biederman" , Christian Brauner , Kees Cook , Jann Horn , Jonathan Corbet , Alexander Viro , Andrew Morton , Alexey Dobriyan , Thomas Gleixner , Oleg Nesterov , Frederic Weisbecker , Andrei Vagin , Ingo Molnar , "Peter Zijlstra (Intel)" , Yuyang Du , David Hildenbrand , Sebastian Andrzej Siewior , Anshuman Khandual , David Howells , James Morris , Shakeel Butt , Jason Gunthorpe , Christian Kellner , Andrea Arcangeli , Aleksa Sarai , "Dmitry V. Levin" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "stable@vger.kernel.org" , "linux-api@vger.kernel.org" References: <077b63b7-6f5e-aa8e-bf96-a586b481cc46@hotmail.de> Message-ID: Date: Fri, 20 Mar 2020 21:24:50 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 In-Reply-To: <077b63b7-6f5e-aa8e-bf96-a586b481cc46@hotmail.de> Content-Language: en-US X-ClientProxiedBy: ZR0P278CA0015.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:16::25) To AM6PR03MB5170.eurprd03.prod.outlook.com (2603:10a6:20b:ca::23) X-Microsoft-Original-Message-ID: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [192.168.1.101] (92.77.140.102) by ZR0P278CA0015.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:16::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2835.15 via Frontend Transport; Fri, 20 Mar 2020 20:24:52 +0000 X-Microsoft-Original-Message-ID: X-TMN: [C6UtGPhiEDoOWZeg17+HeAeDyltOzGuO] X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 49 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 6fcdf3b4-8986-4738-e45d-08d7cd0cbfbf X-MS-TrafficTypeDiagnostic: DB3EUR04HT021: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bR+gXeYFhS1KWVb2KormQIajtSUbhoc2qfSfTd1cFf9VrBfAxanqwPUloWPuOo4SDdHfLN5JkDmUULyzoKp0bt4khz+jBH7pKjeKcr1sZqWvKuCCtDjoRaMt+ad/FfBC/gCiF5LdtkvWKrgTn+LQslsIiGllJb2SCg4d8sDY4h4FDCDpOPWv/h+fTRdKZ40v X-MS-Exchange-AntiSpam-MessageData: tzuSPZVlTP7pcBstDl0CtCDS+rU8j44jxa3awH3HV/ez2jqm5elMyriH8wzNY66L602KRiLekRHt91T9fOIeCNQsDvvALB/8ff3nDiUA4qmK0ReTJPi0i/EG1wU6Yer0fNW8e/ZJB0NUDP7RIgPnFw== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6fcdf3b4-8986-4738-e45d-08d7cd0cbfbf X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2020 20:24:53.9478 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB3EUR04HT021 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This makes the code clearer and makes it easier to implement a mutex that is not taken over any locations that may block indefinitely waiting for userspace. Signed-off-by: "Eric W. Biederman" Reviewed-by: Bernd Edlinger Reviewed-by: Kees Cook Acked-by: Christian Brauner Reviewed-by: Kirill Tkhai --- fs/exec.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index c3f3479..ff74b9a 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1194,6 +1194,23 @@ static int de_thread(struct task_struct *tsk) flush_itimer_signals(); #endif + BUG_ON(!thread_group_leader(tsk)); + return 0; + +killed: + /* protects against exit_notify() and __exit_signal() */ + read_lock(&tasklist_lock); + sig->group_exit_task = NULL; + sig->notify_count = 0; + read_unlock(&tasklist_lock); + return -EAGAIN; +} + + +static int unshare_sighand(struct task_struct *me) +{ + struct sighand_struct *oldsighand = me->sighand; + if (refcount_read(&oldsighand->count) != 1) { struct sighand_struct *newsighand; /* @@ -1210,23 +1227,13 @@ static int de_thread(struct task_struct *tsk) write_lock_irq(&tasklist_lock); spin_lock(&oldsighand->siglock); - rcu_assign_pointer(tsk->sighand, newsighand); + rcu_assign_pointer(me->sighand, newsighand); spin_unlock(&oldsighand->siglock); write_unlock_irq(&tasklist_lock); __cleanup_sighand(oldsighand); } - - BUG_ON(!thread_group_leader(tsk)); return 0; - -killed: - /* protects against exit_notify() and __exit_signal() */ - read_lock(&tasklist_lock); - sig->group_exit_task = NULL; - sig->notify_count = 0; - read_unlock(&tasklist_lock); - return -EAGAIN; } char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) @@ -1264,14 +1271,20 @@ int flush_old_exec(struct linux_binprm * bprm) int retval; /* - * Make sure we have a private signal table and that - * we are unassociated from the previous thread group. + * Make this the only thread in the thread group. */ retval = de_thread(me); if (retval) goto out; /* + * Make the signal table private. + */ + retval = unshare_sighand(me); + if (retval) + goto out; + + /* * Must be called _before_ exec_mmap() as bprm->mm is * not visibile until then. This also enables the update * to be lockless.