Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2184217pxk; Mon, 14 Sep 2020 06:59:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyHs2qxkwOjn7IBzVs97b5HQ9mt6D2o79ZCJYq9wWceEcjERGOY38OCpNtNnxtZJpZJ6Hc5 X-Received: by 2002:a50:fd19:: with SMTP id i25mr17612606eds.142.1600091983352; Mon, 14 Sep 2020 06:59:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600091983; cv=none; d=google.com; s=arc-20160816; b=UgCA7FkOWmCCN62iemoKa1MeWk5T9NF3sqA7AnTB/Dyq27XdBroxMjV4s13oMzz5CO NhnGiHxtzS+y1J/lsKk5k4a52xBvqpaDwgNNBeEidJrvU0NXzsmqGy/MKqKUvTa7FOza Ijy37oqfgcWRu8wA+Tit6zvvHA6UcLR/rtapa2CXFll9vZhZzbS8FTwvns5T1qRS1xaU 43Q+kGU2mcWH2lx1jG/WNMlMLC/dQu7fPJWxRUcKaJ7kmbu16tYK7KzzXytrv990eBYS lGzxFT/Mt8+ZmwnYSDB6FUbpLETDuR7tPGCwPq39oneOIQh8J6y0/0Z0+Ng0PHEkvXLM wZ/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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject:reply-to; bh=ewMke04ovOwIXwkrPYBefYx2xq5/6LUhX8aaiAacjXc=; b=Oo/tcegNzEsgA59QEdZSnZiNHAdcGtUbR6PBJhtbq/tmkqSvYgjiIx+tPDCZH6za+X cssFmKfJ8URDlYk9PnT0DyfeilJaIMh6MaUiTrm8Vy4kNARNpAqAzFOP9yRUlco/zbbv TUSpa/E/VGv1/91/mYw424wp8S3dOUHL1BfyaC+TiegHtA8SbXyJB+9PjCwXhU+tvpbb oHwZc3KAPJzdACl+xC2JjWt9Nls7vuYy4FOves/qflLYbeXM7G5MOAE/veMCn6+4rfov MSuQ/pcDeze76VZPC2/sYRKNRHg3EqpZVFKuxOTkHowsWmUxufoYf8QSpSBtSTcMbK+q UnEg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u59si8093520edc.226.2020.09.14.06.59.20; Mon, 14 Sep 2020 06:59:43 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726802AbgINN6g (ORCPT + 99 others); Mon, 14 Sep 2020 09:58:36 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:34258 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726726AbgINN5S (ORCPT ); Mon, 14 Sep 2020 09:57:18 -0400 Received: by mail-wm1-f66.google.com with SMTP id l15so5947826wmh.1; Mon, 14 Sep 2020 06:56:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=ewMke04ovOwIXwkrPYBefYx2xq5/6LUhX8aaiAacjXc=; b=r8R/6uveUhclcwaeSSDV5A34Z/hU2GY2yAc6eOWnWQ9cq4C1OTyEDXZAdMjRO5nVA2 tOZjGLbSsHpz2A+8ceh56cvGjxTN5nd5jPPrGivqGx4/wnP4i0xZoUDXRhhGdTNY47ky yQ89LA6Oz+fnc+bIbF41ETYXvXTSvn5zB7PArmPcmzJkedaZDygDXcWtKsZHd+i1sn3J DsbSPfEkVcDvWv0gk5R1/7YIDcxhXOv6GrRAiz9CUObdg0rgkO31r7mfJ59ykH7l+27c PXjQIzme88UiEruKp3uSSwQc66ESM0gx5p3JHat6t/jm7GrBRZ408UMoXzVPbzIU4AqO KJxA== X-Gm-Message-State: AOAM531uF/33VNCl82H1eaTuRZ1h0+MeWfsXVxxdYNgpKvk0nPknmmJQ tufXGz4oXRNmwhuXuwO8bBk= X-Received: by 2002:a7b:c925:: with SMTP id h5mr14679527wml.28.1600091785477; Mon, 14 Sep 2020 06:56:25 -0700 (PDT) Received: from [10.9.0.22] ([185.248.161.177]) by smtp.gmail.com with ESMTPSA id b11sm21289595wrt.38.2020.09.14.06.56.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2020 06:56:24 -0700 (PDT) Reply-To: alex.popov@linux.com Subject: Re: [External] Re: [PATCH v2] stackleak: Fix a race between stack erasing sysctl handlers To: Muchun Song Cc: Kees Cook , Masami Hiramatsu , Steven Rostedt , miguel.ojeda.sandonis@gmail.com, LKML , Luis Chamberlain , Iurii Zaikin , linux-fsdevel@vger.kernel.org, mike.kravetz@oracle.com References: <20200828031928.43584-1-songmuchun@bytedance.com> <8c288fd4-2ef7-ca47-1f3b-e4167944b235@linux.com> From: Alexander Popov Autocrypt: addr=alex.popov@linux.com; prefer-encrypt=mutual; keydata= mQINBFX15q4BEADZartsIW3sQ9R+9TOuCFRIW+RDCoBWNHhqDLu+Tzf2mZevVSF0D5AMJW4f UB1QigxOuGIeSngfmgLspdYe2Kl8+P8qyfrnBcS4hLFyLGjaP7UVGtpUl7CUxz2Hct3yhsPz ID/rnCSd0Q+3thrJTq44b2kIKqM1swt/F2Er5Bl0B4o5WKx4J9k6Dz7bAMjKD8pHZJnScoP4 dzKPhrytN/iWM01eRZRc1TcIdVsRZC3hcVE6OtFoam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+S5SPaMRFUaK8S3j1vYC0wZJ99rrmQbcbYMh C2o0k4pSejaINmgyCajVOhUhln4IuwvZke1CLfXe1i3ZtlaIUrxfXqfYpeijfM/JSmliPxwW BRnQRcgS85xpC1pBUMrraxajaVPwu7hCTke03v6bu8zSZlgA1rd9E6KHu2VNS46VzUPjbR77 kO7u6H5PgQPKcuJwQQ+d3qa+5ZeKmoVkc2SuHVrCd1yKtAMmKBoJtSku1evXPwyBzqHFOInk mLMtrWuUhj+wtcnOWxaP+n4ODgUwc/uvyuamo0L2Gp3V5ItdIUDO/7ZpZ/3JxvERF3Yc1md8 5kfflpLzpxyl2fKaRdvxr48ZLv9XLUQ4qNuADDmJArq/+foORAX4BBFWvqZQKe8a9ZMAvGSh uoGUVg4Ks0uC4IeG7iNtd+csmBj5dNf91C7zV4bsKt0JjiJ9a4D85dtCOPmOeNuusK7xaDZc gzBW8J8RW+nUJcTpudX4TC2SGeAOyxnM5O4XJ8yZyDUY334seDRJWtS4wRHxpfYcHKTewR96 IsP1USE+9ndu6lrMXQ3aFsd1n1m1pfa/y8hiqsSYHy7JQ9Iuo9DxysOj22UNOmOE+OYPK48D j3lCqPk= Message-ID: <2f347fde-6f8d-270b-3886-0d106fcc5a46@linux.com> Date: Mon, 14 Sep 2020 16:56:21 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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? [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