From patchwork Sun Mar 8 12:58:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 11425613 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 5052C924 for ; Sun, 8 Mar 2020 12:58:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0315C2087F for ; Sun, 8 Mar 2020 12:58:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0315C2087F 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 2184D6B0005; Sun, 8 Mar 2020 08:58:38 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1A1F46B0006; Sun, 8 Mar 2020 08:58:38 -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 043486B0007; Sun, 8 Mar 2020 08:58:37 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0183.hostedemail.com [216.40.44.183]) by kanga.kvack.org (Postfix) with ESMTP id DB2136B0005 for ; Sun, 8 Mar 2020 08:58:37 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 9AB42181AC9C6 for ; Sun, 8 Mar 2020 12:58:37 +0000 (UTC) X-FDA: 76572199074.14.van02_2e22460639661 X-Spam-Summary: 2,0,0,b3f2b2873e6948fb,d41d8cd98f00b204,bernd.edlinger@hotmail.de,,RULES_HIT:1:10:41:355:379:800:960:966:988:989:1260:1261:1277:1311:1313:1314:1345:1359:1437:1513:1515:1516:1518:1521:1543:1593:1594:1683:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2553:2559:2562:2737:3138:3139:3140:3141:3142:3355:3865:3866:3867:3868:3870:3871:3872:3873:3874:4385:4605:5007:6119:6742:6743:7652:10004:10400:11026:11232:11233:11658:11914:12043:12160:12291:12296:12297:12519:12555:12683:12895:13163:13229:14096:14097:14181:14394:14659:14721:14877:21080:21222:21444:21627:21990:30054:30055:30090,0,RBL:40.92.65.81:@hotmail.de:.lbl8.mailshell.net-66.100.201.100 62.18.84.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: van02_2e22460639661 X-Filterd-Recvd-Size: 10784 Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-oln040092065081.outbound.protection.outlook.com [40.92.65.81]) by imf27.hostedemail.com (Postfix) with ESMTP for ; Sun, 8 Mar 2020 12:58:36 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bTOrKTspOKyvQYkVi23/gFWakov//Kc6b0t/jpVPhxsCVEHHwdCL9/Iap3kbpwibTK7m8hWjw7tL4IbsXsvd8OwdnDtuYds5RInkSC7WvaKX2p975j3iqukl2ZrTMkZxz9E/zT60sUh45J4v/lrGzM+Kmm0WOOeL6UrLBiShPrOrbMX1/JA0/XUIqn5VsARcrTF69nOVzmt04O6kVloMFAtJNWiHIrnaz87s8cFq5QGczzWPdZRukkxV80/VXqXUcoA8tGm0CfCgu7pIngl70psHg3kv5GzvNZKA6hzyiIsSeJBF9SGfxdHT1h/A8tdAS157sdsyd0C0E0iZ4yUK0Q== 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=X/9Aakd6wMlI9i+v8qvX3GAes78Rrq/6ZPfa1pAQvEw=; b=mHLPtR2r8dAMgB7YL6IBvmmZ8AzR7DL+BxBQtEja1OYdNopkDXJEB81TLsFPiUCDnXNsla7aIXJgvkxyd+30ONYySfURKZd6n85ghlYBNyful6LU37yq0ieTiYWfTRmP1zr/y3/O5hyz6wqMyp+NmYVJUapUVsej4ZkI6G1EXuvor7AnPc4ADpMEPKLvqxIQURgTI+tbZ8gPGV28AkmCiQzO4IhLSfcYsoxBdcxJE9RywfSKbKmcOQkwCxje7Zy0kSCce8ceF2io/Ak5Xk0zq2E6BOeSNC7PV5ZavgpeYliAH9aFjWG2jBBCN2Q7f1F5MU3Lrf/2Eu/oD6br4NIcKQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from HE1EUR01FT016.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e18::33) by HE1EUR01HT122.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e18::257) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2793.11; Sun, 8 Mar 2020 12:58:33 +0000 Received: from AM6PR03MB5170.eurprd03.prod.outlook.com (10.152.0.55) by HE1EUR01FT016.mail.protection.outlook.com (10.152.0.169) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2793.11 via Frontend Transport; Sun, 8 Mar 2020 12:58:33 +0000 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.2772.019; Sun, 8 Mar 2020 12:58:33 +0000 Received: from [192.168.1.101] (92.77.140.102) by FRYP281CA0014.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2793.15 via Frontend Transport; Sun, 8 Mar 2020 12:58:31 +0000 From: Bernd Edlinger To: "Eric W. Biederman" CC: 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 , Greg Kroah-Hartman , 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" Subject: [PATCH] exec: make de_thread alloc new signal struct earlier Thread-Topic: [PATCH] exec: make de_thread alloc new signal struct earlier Thread-Index: AQHV9UlFwrz7zF+kPUydVgrwvmKDSg== Date: Sun, 8 Mar 2020 12:58:33 +0000 Message-ID: References: <87v9nlii0b.fsf@x220.int.ebiederm.org> <87a74xi4kz.fsf@x220.int.ebiederm.org> <87r1y8dqqz.fsf@x220.int.ebiederm.org> <87tv32cxmf.fsf_-_@x220.int.ebiederm.org> <87imjicxjw.fsf_-_@x220.int.ebiederm.org> <87k13yawpp.fsf@x220.int.ebiederm.org> <87sgil87s3.fsf@x220.int.ebiederm.org> <87a74t86cs.fsf@x220.int.ebiederm.org> <87v9nh6koh.fsf@x220.int.ebiederm.org> In-Reply-To: <87v9nh6koh.fsf@x220.int.ebiederm.org> Accept-Language: en-US, en-GB, de-DE Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: FRYP281CA0014.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10::24) To AM6PR03MB5170.eurprd03.prod.outlook.com (2603:10a6:20b:ca::23) x-incomingtopheadermarker: OriginalChecksum:3D85B0069CFCB3CB2F8D57AD2918C52AEFCFFF7E73FBBC4D30DE4B8A99032FC1;UpperCasedChecksum:B6FB0BE11F08487B16A09B3755431B739A34309DEE7FECEEE50863F42FF0B3C8;SizeAsReceived:9840;Count:50 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [eZQY9KO2YxHolo9GQIy/PxCHKI6mNLdh] x-microsoft-original-message-id: x-ms-publictraffictype: Email x-incomingheadercount: 50 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 289a7201-5028-4e5b-d380-08d7c36067f8 x-ms-traffictypediagnostic: HE1EUR01HT122: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 3/HYr4NjwhQWzo/yAEXqj+7Q1GN7r80LOLNy108m5v7/Y25AbCP2mGYGE/KCNktyxrRAcN8QNFD4okRA5EwLsSgIcYgKJGSN3PfpOo60qJFd2WRyQxcj9k6SUXlP1H+s95Rs7cyQYChiH7qKwsqSSjoKuf0UvgNraS9pZC/zEd+byS9veyppgv5b2HsUzDBr x-ms-exchange-antispam-messagedata: B19g5ARpdk5V7AtZggLF0dkG1MXCqwutFgltE3bsPV1lMw2yBSvvNJ2sQB6hEn50w1HJ9OHwRm58pIoC/qvBwOVWPmJWz2EjuxCkraRcSrDkCaDtQAtc9+CCJErIHfVTnXuGs1PQfxRba1+ciIXM/g== x-ms-exchange-transport-forked: True Content-ID: <5DBB198BA9313544AD126FE23828D207@eurprd03.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 289a7201-5028-4e5b-d380-08d7c36067f8 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Mar 2020 12:58:33.2387 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1EUR01HT122 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: It was pointed out that de_thread may return -ENOMEM when it already terminated threads, and returning an error from execve, except when a fatal signal is being delivered is not an option any more. Allocate the memory for the signal table earlier, and make sure that -ENOMEM is returned before the unrecoverable actions are started. Signed-off-by: Bernd Edlinger --- Eric, what do you think, might this be helpful to move the "point of no return" lower, and simplify your patch? fs/exec.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 74d88da..a0328dc 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1057,16 +1057,26 @@ static int exec_mmap(struct mm_struct *mm) * disturbing other processes. (Other processes might share the signal * table via the CLONE_SIGHAND option to clone().) */ -static int de_thread(struct task_struct *tsk) +static int de_thread(void) { + struct task_struct *tsk = current; struct signal_struct *sig = tsk->signal; struct sighand_struct *oldsighand = tsk->sighand; spinlock_t *lock = &oldsighand->siglock; + struct sighand_struct *newsighand = NULL; if (thread_group_empty(tsk)) goto no_thread_group; /* + * This is the last time for an out of memory error. + * After this point only fatal signals are are okay. + */ + newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); + if (!newsighand) + return -ENOMEM; + + /* * Kill all other threads in the thread group. */ spin_lock_irq(lock); @@ -1076,7 +1086,7 @@ static int de_thread(struct task_struct *tsk) * return so that the signal is processed. */ spin_unlock_irq(lock); - return -EAGAIN; + goto err_free; } sig->group_exit_task = tsk; @@ -1191,14 +1201,16 @@ static int de_thread(struct task_struct *tsk) #endif if (refcount_read(&oldsighand->count) != 1) { - struct sighand_struct *newsighand; /* * This ->sighand is shared with the CLONE_SIGHAND * but not CLONE_THREAD task, switch to the new one. */ - newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); - if (!newsighand) - return -ENOMEM; + if (!newsighand) { + newsighand = kmem_cache_alloc(sighand_cachep, + GFP_KERNEL); + if (!newsighand) + return -ENOMEM; + } refcount_set(&newsighand->count, 1); memcpy(newsighand->action, oldsighand->action, @@ -1211,7 +1223,8 @@ static int de_thread(struct task_struct *tsk) write_unlock_irq(&tasklist_lock); __cleanup_sighand(oldsighand); - } + } else if (newsighand) + kmem_cache_free(sighand_cachep, newsighand); BUG_ON(!thread_group_leader(tsk)); return 0; @@ -1222,6 +1235,8 @@ static int de_thread(struct task_struct *tsk) sig->group_exit_task = NULL; sig->notify_count = 0; read_unlock(&tasklist_lock); +err_free: + kmem_cache_free(sighand_cachep, newsighand); return -EAGAIN; } @@ -1262,7 +1277,7 @@ int flush_old_exec(struct linux_binprm * bprm) * Make sure we have a private signal table and that * we are unassociated from the previous thread group. */ - retval = de_thread(current); + retval = de_thread(); if (retval) goto out;