Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp349759imn; Wed, 3 Aug 2022 06:35:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uuvQozI++7yPCy203XokM6u4r03Cpr1shCwORQf4GuDqG2XX/W/RrlOisn0yMLwXTkXQbw X-Received: by 2002:a63:2a0e:0:b0:41a:f82d:72f6 with SMTP id q14-20020a632a0e000000b0041af82d72f6mr21006655pgq.42.1659533704642; Wed, 03 Aug 2022 06:35:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659533704; cv=none; d=google.com; s=arc-20160816; b=aCdUGQ/C2Ynmx4A8pq/dXphQq3owWN9I0hzGCCm+4XBGanx4buAJRpcbptIvNIAnQX a32FU2uufZP0hBYYafDL8UbzFeoBKXQa5mm1hidTGiP7KHnh4XoTbCLZ9jJNgzbuJCWI yVintxHxB5TqkUmuOu4vJpVHWlDDc/ymE70YB4jXh2UAIEdnCBK5wtzMKDboLTecZTLZ jRhErT8aMQZQQNR6pVHZ+HlU61iZQj99YLyGRbgnDoG/CmkB8ADb1StUv/owMoszIleg Q7uJjJTofx+edMaf09cvUtR3D5CWYa0iAmPIayZoJMQt0A9QkB88rQ0jKrdTJDkEJGDE oilw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=75c/vhgfgwtPaSPTZIQ1nQmAabAmAJZb+69obINC/dg=; b=nwDRpu/AXzZ52PGM80auuFzrm3P7HsO5tHkhycF+NHfB7EITkeSzcIgOU7leWHkTfI B+dkEb2xfElQnvfvXVDzpLV6NaoQuJOqzDzSqNG8yZkBUTpIbt3QzpWjz9Kp+Ab7DyFh Zj0XJ1qkKCWTNDtEO2ogfjY/jChIsWNP0FjfItyYACMEc7YbDwtv9IeVg5nVL6V3Palw B4g62UXYIMJCG+oCRGBxX3oi6MDM+jXzUPSVbmzgyxB8hJ9spjGqgNTdgUs/BiYX6fD7 hVkXSK/YOurIa6mv2pXqijZHsquCJ+aSvpmIfzOAvFqp/PqwxRILAZO3zjXwD6ZLnAnE jv8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@paul-moore-com.20210112.gappssmtp.com header.s=20210112 header.b=cFH0wtUR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j15-20020a170902da8f00b0016da78aeff7si2921884plx.215.2022.08.03.06.34.48; Wed, 03 Aug 2022 06:35:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@paul-moore-com.20210112.gappssmtp.com header.s=20210112 header.b=cFH0wtUR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236265AbiHCNQR (ORCPT + 99 others); Wed, 3 Aug 2022 09:16:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235851AbiHCNQP (ORCPT ); Wed, 3 Aug 2022 09:16:15 -0400 Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F86A186E1 for ; Wed, 3 Aug 2022 06:16:14 -0700 (PDT) Received: by mail-ot1-x335.google.com with SMTP id g19-20020a9d1293000000b0061c7bfda5dfso12196641otg.1 for ; Wed, 03 Aug 2022 06:16:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paul-moore-com.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=75c/vhgfgwtPaSPTZIQ1nQmAabAmAJZb+69obINC/dg=; b=cFH0wtURz57x5BNd0aYndz1RkpiNpoA3tV57uUKqVtm2xTlZaGsCJnUX0u9fIAwroZ E8gzehLhLbXFO/Af26SlTxo8Lhvib3iRfHtSToAKw8nrKqxd38NOI1fPjENQ5jE+rGhN Wh99bf7eb9+Jm8YoT5Z0Easx3yDYJMnTUUCzO8iwAzUOauHW3BcLW1QRtlkF+vlVe+I+ YMg+ODSyWGxFkpFmkgEeMTpuer9sQ6EQiAaibdkXHZ8JGIULNCq8dKb7D3yZBUMKo09r APAdYMR4bpFQ7bD9XQ55yS+FV9HIIKWJ2dOmyerYGJR2gB+Z1z1blewa7sdAaVAe0rV5 0Pvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=75c/vhgfgwtPaSPTZIQ1nQmAabAmAJZb+69obINC/dg=; b=KTBFi2fLKpB1JqXlbmaveM44y1kNkDplDVc4k0ZuUmx2JoGYvU9E8NxXqNHRyRRMg3 6X2Dsrm+dVihclCkPKtqpjL1IiOIy2EH6+QLdi/Sc7X/hdznTeqbIm2H9U14v9nMQMPE IpdxeMRO8Rot4dAsutttsy9/eeR+8s7mGm6Dz2v/DDUNM9ibCZDSvJ8dqMNadouj7+/1 2RywHWcCR6FZ3AUq8DbV2qfm7NvrS8yu0ehr52tKd+g/ZY1rvQMSB0ouMBcyWTi3tXw9 ZbLgct6UjHdZxUhoqgjyc8A0GBcklaCmZaagAZwseV90mGYSpnk98MZQDuFSkAnQ4EIm d1CA== X-Gm-Message-State: ACgBeo3CYNkqNnrv2Vzz9kq6/HC/75PozUyNAG70B3iRvbUffahaA/AF oSQuVY40RJr2pQ9S2ApjF7nhPH5m5nWzFLZPIktl X-Received: by 2002:a9d:7a99:0:b0:629:805:bca4 with SMTP id l25-20020a9d7a99000000b006290805bca4mr5874553otn.26.1659532573884; Wed, 03 Aug 2022 06:16:13 -0700 (PDT) MIME-Version: 1.0 References: <20220803050230.30152-1-yepeilin.cs@gmail.com> In-Reply-To: <20220803050230.30152-1-yepeilin.cs@gmail.com> From: Paul Moore Date: Wed, 3 Aug 2022 09:16:02 -0400 Message-ID: Subject: Re: [PATCH] audit, io_uring, io-wq: Fix memory leak in io_sq_thread() and io_wqe_worker() To: Peilin Ye Cc: Jens Axboe , Pavel Begunkov , Eric Paris , Peilin Ye , io-uring@vger.kernel.org, linux-kernel@vger.kernel.org, linux-audit@redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 3, 2022 at 1:03 AM Peilin Ye wrote: > > From: Peilin Ye > > Currently @audit_context is allocated twice for io_uring workers: > > 1. copy_process() calls audit_alloc(); > 2. io_sq_thread() or io_wqe_worker() calls audit_alloc_kernel() (which > is effectively audit_alloc()) and overwrites @audit_context, > causing: > > BUG: memory leak > unreferenced object 0xffff888144547400 (size 1024): > <...> > hex dump (first 32 bytes): > 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 ................ > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ > backtrace: > [] audit_alloc+0x133/0x210 > [] copy_process+0xcd3/0x2340 > [] create_io_thread+0x63/0x90 > [] create_io_worker+0xb4/0x230 > [] io_wqe_enqueue+0x248/0x3b0 > [] io_queue_iowq+0xba/0x200 > [] io_queue_async+0x113/0x180 > [] io_req_task_submit+0x18f/0x1a0 > [] io_apoll_task_func+0xdd/0x120 > [] tctx_task_work+0x11f/0x570 > [] task_work_run+0x7e/0xc0 > [] get_signal+0xc18/0xf10 > [] arch_do_signal_or_restart+0x2b/0x730 > [] exit_to_user_mode_prepare+0x5e/0x180 > [] syscall_exit_to_user_mode+0x12/0x20 > [] do_syscall_64+0x40/0x80 > > Then, > > 3. io_sq_thread() or io_wqe_worker() frees @audit_context using > audit_free(); > 4. do_exit() eventually calls audit_free() again, which is okay > because audit_free() does a NULL check. > > Free the old @audit_context first in audit_alloc_kernel(), and delete > the redundant calls to audit_free() for less confusion. > > Fixes: 5bd2182d58e9 ("audit,io_uring,io-wq: add some basic audit support to io_uring") > Cc: stable@vger.kernel.org > Signed-off-by: Peilin Ye > --- > Hi all, > > A better way to fix this memleak would probably be checking > @args->io_thread in copy_process()? Something like: > > if (args->io_thread) > retval = audit_alloc_kernel(); > else > retval = audit_alloc(); > > But I didn't want to add another if to copy_process() for this bugfix. > Please suggest, thanks! Thanks for the report and patch! I'll take a closer look at this today and get back to you. > fs/io-wq.c | 1 - > fs/io_uring.c | 2 -- > kernel/auditsc.c | 1 + > 3 files changed, 1 insertion(+), 3 deletions(-) -- paul-moore.com