Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2202798pxk; Mon, 14 Sep 2020 07:19:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRXRTYIPeU8ijtbjW0CDuVcToeffeFJgK+HLT1Z3H2hQB8Mlmrmr4ZHsYAtY8fuNgVB+XI X-Received: by 2002:a17:906:552:: with SMTP id k18mr2930724eja.482.1600093140506; Mon, 14 Sep 2020 07:19:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600093140; cv=none; d=google.com; s=arc-20160816; b=fKaZ6/6YBDhOPPZOWz9LU3leLdUzKJRJZy5EP5ojCdXtu6AJYjRfIjy9AdSuJR/0oP zd1dwI4aIsPNfBU+Il8tURggpMpC1zRzM69TYqyXBVCEu27DBr0PG3eOa72Dj4cAZioL dUFBC3WE98ZxsBuNfXPWWibEXeDvVAciebJHalYnAKUNclRphs0Ujw7BOjnKq5C0sD/n YudWN74axBDwkt6qoe14TUMm/GZZ6W8xmVqXzAXzsJU3swCZmjoYqnimmfS281eDdROB Kjkl8nC0oRl4PEBehMRdaHlUgHtQuO0G5OzjNWShnUlXVkr1Aiq48NAA2AuX9Vzlh9/g XH4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=PRqXshFDT+vnZmomkxjfH2FYDG36/bbMKP3d6/TVV9M=; b=di4ea6sLGETjKYr8sHWdWBRkNRCqkl75kJ+4EiTPEqJFak21+qUwY/PlDGxEGGND3z DFMmyCElCZIQfmAWoKUFqxJEmh+gwTUbe3VR5+HV9stXSRVvgXsMyVFLz0l9Mg3Dq8lJ ggPqLXbyKBguM9pM6Rb0kHGjvAWlSvIFhBkLmoqNEkfJPpDyRjTN0WXPMI90xZOz02lS T09ks5Bd2DzVV+NcS/NzoBA/ieCcNNEqhoYX7S25VAo3NXe8LjqOeGcRK8ugfYPhjxqL 059BgXxoOdbl0IOO7w2OMztZEIMW+nzN1rcFzaD7/KLneV4ni3xEBdDtEV4c5Ipf+wcF CYBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=SA6T4f5w; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cy19si7564082edb.313.2020.09.14.07.18.38; Mon, 14 Sep 2020 07:19:00 -0700 (PDT) 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=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=SA6T4f5w; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726724AbgINOKJ (ORCPT + 99 others); Mon, 14 Sep 2020 10:10:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726385AbgINOKB (ORCPT ); Mon, 14 Sep 2020 10:10:01 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11E2FC06178A for ; Mon, 14 Sep 2020 07:10:01 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id o16so5637413pjr.2 for ; Mon, 14 Sep 2020 07:10:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PRqXshFDT+vnZmomkxjfH2FYDG36/bbMKP3d6/TVV9M=; b=SA6T4f5wMMABu+t8mm80Smz6yCnXmOq4zhzbzYWqAt4U6dd1B4KI5EWZkoOvr2B1gU QgoGOV3mSA281/6F47ccWVGpociAdB0LrUQA4l3YR2SeyfI0wSm9qf8US52gEW0KQF2y H6qrSGtKEVOqURphrLheizNfZf4sq3h89O0/czzgZVCEWsLf/BKneKHsAOwbCHap5+Wp NJHyoJF/o+ebSmCYqBF+eB4fDR9e+bO/d/Q19gWKyxze0kMq7WVFrF/ap0HI6pPiOytZ LjjmKZXYYXnmskJR37USBx1R4oRyBc/NmCl86+YGHoIKRxnuD4UbSgs6aAfL3sGcD1Yi bnVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PRqXshFDT+vnZmomkxjfH2FYDG36/bbMKP3d6/TVV9M=; b=BtaMcY4La3SmyXKeJJyZqCd4gtCHbzrFmtBkg/cmwNDanrXci8f0aimycwSju19wqf bF1MRLTSBX8Lqm8QE15y52MjCgfVeW5M6B+PbOW++W6+nvbGHOjc9n/b9GstN7maJoPB JyMQMXIzX/slj59bvQpce1XdeHRDc9RCMMPsJp50t2uS5xtPQ3TDiK+llhDSTQD8xDce lYHH7u7+XPj87w+/0xiO8ESHThfr4uxFQnCg4FQ6f1vo4aug/lUf+4r+5Ibnwu6Ed8zB 4gAE0pbfpvQBvS8LgxOfE/uQDH0wlxn29bfNG7KdYduRP+JUbHZXgUwyq/TfiCw+xCW7 qR+g== X-Gm-Message-State: AOAM5324eO2WUCy3m4puD8pdLZLYZzozL4q5FDOi0dK+HQETYhLr4Hbv zlb+2mEzplWy+XBCK/K8pdqbxmPzGvpUxDk3j3gvSQ== X-Received: by 2002:a17:90a:fa94:: with SMTP id cu20mr14131322pjb.147.1600092600608; Mon, 14 Sep 2020 07:10:00 -0700 (PDT) MIME-Version: 1.0 References: <20200828031928.43584-1-songmuchun@bytedance.com> <8c288fd4-2ef7-ca47-1f3b-e4167944b235@linux.com> <2f347fde-6f8d-270b-3886-0d106fcc5a46@linux.com> In-Reply-To: <2f347fde-6f8d-270b-3886-0d106fcc5a46@linux.com> From: Muchun Song Date: Mon, 14 Sep 2020 22:09:24 +0800 Message-ID: Subject: Re: [External] Re: [PATCH v2] stackleak: Fix a race between stack erasing sysctl handlers To: alex.popov@linux.com Cc: Kees Cook , Masami Hiramatsu , Steven Rostedt , miguel.ojeda.sandonis@gmail.com, LKML , Luis Chamberlain , Iurii Zaikin , linux-fsdevel@vger.kernel.org, Mike Kravetz Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 14, 2020 at 9:56 PM Alexander Popov wrote: > > On 07.09.2020 16:53, Muchun Song wrote: > > On Mon, Sep 7, 2020 at 7:24 PM Alexander Popov wrote: > >> > >> On 07.09.2020 05:54, Muchun Song wrote: > >>> Hi all, > >>> > >>> Any comments or suggestions? Thanks. > >>> > >>> On Fri, Aug 28, 2020 at 11:19 AM Muchun Song wrote: > >>>> > >>>> There is a race between the assignment of `table->data` and write value > >>>> to the pointer of `table->data` in the __do_proc_doulongvec_minmax() on > >>>> the other thread. > >>>> > >>>> CPU0: CPU1: > >>>> proc_sys_write > >>>> stack_erasing_sysctl proc_sys_call_handler > >>>> table->data = &state; stack_erasing_sysctl > >>>> table->data = &state; > >>>> proc_doulongvec_minmax > >>>> do_proc_doulongvec_minmax sysctl_head_finish > >>>> __do_proc_doulongvec_minmax unuse_table > >>>> i = table->data; > >>>> *i = val; // corrupt CPU1's stack > >> > >> Hello everyone! > >> > >> As I remember, I implemented stack_erasing_sysctl() very similar to other sysctl > >> handlers. Is that issue relevant for other handlers as well? > > > > Yeah, it's very similar. But the difference is that others use a > > global variable as the > > `table->data`, but here we use a local variable as the `table->data`. > > The local variable > > is allocated from the stack. So other thread could corrupt the stack > > like the diagram > > above. > > Hi Muchun, > > I don't think that the proposed copying of struct ctl_table to local variable is > a good fix of that issue. There might be other bugs caused by concurrent > execution of stack_erasing_sysctl(). I can not figure out how the bug happened when there is concurrent execution of stack_erasing_sysctl(). > > I would recommend using some locking instead. > > But you say there are other similar issues. Should it be fixed on higher level > in kernel/sysctl.c? Yeah, we can see the same issue here. https://lkml.org/lkml/2020/8/22/105. I agree with you. Maybe a fix on the higher level is a good choice in kernel/sysctl.c. If someone also agrees with this solution, I can do this work. > > [Adding more knowing people to CC] > > Thanks! > > >> Muchun, could you elaborate how CPU1's stack is corrupted and how you detected > >> that? Thanks! > > > > Why did I find this problem? Because I solve another problem which is > > very similar to > > this issue. You can reference the following fix patch. Thanks. > > > > https://lkml.org/lkml/2020/8/22/105 > >> > >>>> Fix this by duplicating the `table`, and only update the duplicate of > >>>> it. > >>>> > >>>> Fixes: 964c9dff0091 ("stackleak: Allow runtime disabling of kernel stack erasing") > >>>> Signed-off-by: Muchun Song > >>>> --- > >>>> changelogs in v2: > >>>> 1. Add more details about how the race happened to the commit message. > >>>> > >>>> kernel/stackleak.c | 11 ++++++++--- > >>>> 1 file changed, 8 insertions(+), 3 deletions(-) > >>>> > >>>> diff --git a/kernel/stackleak.c b/kernel/stackleak.c > >>>> index a8fc9ae1d03d..fd95b87478ff 100644 > >>>> --- a/kernel/stackleak.c > >>>> +++ b/kernel/stackleak.c > >>>> @@ -25,10 +25,15 @@ int stack_erasing_sysctl(struct ctl_table *table, int write, > >>>> int ret = 0; > >>>> int state = !static_branch_unlikely(&stack_erasing_bypass); > >>>> int prev_state = state; > >>>> + struct ctl_table dup_table = *table; > >>>> > >>>> - table->data = &state; > >>>> - table->maxlen = sizeof(int); > >>>> - ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); > >>>> + /* > >>>> + * In order to avoid races with __do_proc_doulongvec_minmax(), we > >>>> + * can duplicate the @table and alter the duplicate of it. > >>>> + */ > >>>> + dup_table.data = &state; > >>>> + dup_table.maxlen = sizeof(int); > >>>> + ret = proc_dointvec_minmax(&dup_table, write, buffer, lenp, ppos); > >>>> state = !!state; > >>>> if (ret || !write || state == prev_state) > >>>> return ret; > >>>> -- > >>>> 2.11.0 -- Yours, Muchun