Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3369678pxk; Mon, 7 Sep 2020 10:54:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTi1TiL55p8LjY8dB7kO45qBY7C0wHMsx1oCwA2fdKUVMiic2dG5a4Z7EiervSEVIY9rKw X-Received: by 2002:a17:906:fccb:: with SMTP id qx11mr22808991ejb.429.1599501249574; Mon, 07 Sep 2020 10:54:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599501249; cv=none; d=google.com; s=arc-20160816; b=v92MJb/iyI1b9bcmyZrMebN6MQ9XrkCfdoSV/iQWRUbl2NZkeA3oUrOsY4q0q8JJOt 2+Uz3ATb3tCIK/MUNhYhVJfkAtnZVzKZr93fdoQtZQBGumMJTkmQY/KhqFmiviRQTSL8 lw94uVB48+/wzL7IOAYjnFESKDts31rs/hXradVtN4d8dhgzv1h3QbRD+s90Jed7oh9K 6PgWjZ2OIf9+STo9WTuLEGethUMJowZ0AmHuxalhWEetAOVjL64bB3XWKntA1XPl0+pl rkyr9Wv5+HENZLoZ0nBkzGZsqmCRpMyDZ71ATrDUofiV+PHt3RZTG44tbqzRPqzLURWr PDiA== 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=J4+2saA0WM472pWg2AwLjD2nWWme4gZAQcTGCzryCrc=; b=kC0lzSGEhMQRTKILIWpsGRR6Et0oAt1WqRRUxi6Inbmo8baT5OT1vCm1bdVTcEx36J JJFWVTeWNDLZrmgkMWJFaAEPuj9zErEZgGnWofQrB6GmAMn2d3IfDmh2HDCTkP5kq9eB SZ0p2YTbxEc3FkAswrZc1btOHn9nQBWYAYhkqns8aKTISm914MrawsnIB238fPjVFcQD gypyB+jbZdVnRyOWMbqFRN4ccZvT7bwgHtsGQEq/4t8eVNiEpLrge2pzWBvh+/yWox2o h6+U7n84o2hn1yX2Y3BKvAmSxr5PAtrOrYG634h9lO87Pr2ASZjebC1Bj3f/anIqhJZ3 Hw6g== 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 bm16si10061414edb.114.2020.09.07.10.53.47; Mon, 07 Sep 2020 10:54:09 -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 S1729650AbgIGRw6 (ORCPT + 99 others); Mon, 7 Sep 2020 13:52:58 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:37345 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728964AbgIGLYO (ORCPT ); Mon, 7 Sep 2020 07:24:14 -0400 Received: by mail-wr1-f65.google.com with SMTP id z4so15431078wrr.4 for ; Mon, 07 Sep 2020 04:24:12 -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=J4+2saA0WM472pWg2AwLjD2nWWme4gZAQcTGCzryCrc=; b=FXGuHxB9gwytXP8Qo5WMwdqs4ze/lm13bmwOBaKUmaYfL+KRN3J7MIzhwvy6slYvXn HFXBLLQXU4asaYwSbjKjujKj0uAaaeJS38Rs0oCZ/VwqgXYs5gdeCumuBTGIdIrKE5x3 rezFbIBz5ZEp1/P3rqu9GO0DjdrhQlUWstY6YQXl4RsEWsyUASlCtsTfgMqMrfQ++w/+ 1g7nh3PTW4qLBogl0WJnm9+enZwV+3aIBn7qrdfdBZyNj7sP7LkUF3glRrs18yOMpE3Y cKa0z+uDRPsvRg7FdvFGa89r8454r7Nwt8JbAvtO3HRrSluwfl4AJmIqk9f7Wjllc+Lt p8tw== X-Gm-Message-State: AOAM533bST84MPaxPK5TIBxfKhayervJCgcKBGdCI2661AuNI/bMMGyP w6DwpJ6kWaJ05GlRWRUMuQJevbHHL5Y= X-Received: by 2002:a5d:5042:: with SMTP id h2mr21383630wrt.409.1599477851313; Mon, 07 Sep 2020 04:24:11 -0700 (PDT) Received: from [10.9.0.22] ([185.248.161.177]) by smtp.gmail.com with ESMTPSA id t1sm24718017wmi.16.2020.09.07.04.24.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Sep 2020 04:24:10 -0700 (PDT) Reply-To: alex.popov@linux.com Subject: Re: [PATCH v2] stackleak: Fix a race between stack erasing sysctl handlers To: Muchun Song , Kees Cook , Masami Hiramatsu , Steven Rostedt , miguel.ojeda.sandonis@gmail.com Cc: LKML References: <20200828031928.43584-1-songmuchun@bytedance.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/iWM01eRZRc1TcIdVsRZC3hcVE6OtFoamaYmePDwWTRhmDtWYngbRDVGe3Tl8bT 7BYN7gv7Ikt7Nq2T2TOfXEQqr9CtidxBNsqFEaajbFvpLDpUP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IUrxfXqfYpeijfM/JSmliPxwW BRnQRcgS85xpC1pBUMrraxajaVPwu7hCTke03v6bu8zSZlgA1rd9E6KHu2VNS46VzUPjbR77 kO7u6H5PgQPKcuJwQQ+d3qa+5ZeKmoVkc2SuHVrCd1yKtAMmKBoJtSku1evXPwyBzqHFOInk mLMtrWuUhj+wtcnOWxaP+n4ODgUwc/uvyuamo0L2Gp3V5ItdIUDO/7ZpZ/3JxvERF3Yc1md8 5kfflpLzpxyl2fKaRdvxr48ZLv9XLUQ4qNuADDmJArq/+foORAX4BBFWvqZQKe8a9ZMAvGSh uoGUVg4Ks0uC4IeG7iNtd+csmBj5dNf91C7zV4bsKt0JjiJ9a4D85dtCOPmOeNuusK7xaDZc gzBW8J8RW+nUJcTpudX4TC2SGeAOyxnM5O4XJ8yZyDUY334seDRJWtS4wRHxpfYcHKTewR96 IsP1USE+9ndu6lrMXQ3aFsd1n1m1pfa/y8hiqsSYHy7JQ9Iuo9DxysOj22UNOmOE+OYPK48D j3lCqPk= Message-ID: <8c288fd4-2ef7-ca47-1f3b-e4167944b235@linux.com> Date: Mon, 7 Sep 2020 14:24:08 +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 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? Muchun, could you elaborate how CPU1's stack is corrupted and how you detected that? Thanks! Best regards, Alexander >> 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 >> > >