Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp901976pxa; Thu, 27 Aug 2020 20:21:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3yaJbVGLnA4mmaL+h7MWteVUErnhiTS8/H6lilJ56jm8lgisqHM6WrTKUcLuAA/iGj044 X-Received: by 2002:a05:6402:b45:: with SMTP id bx5mr23331422edb.22.1598584871884; Thu, 27 Aug 2020 20:21:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598584871; cv=none; d=google.com; s=arc-20160816; b=qtt7mGsLI+PPHcHCqe6azVs4XaG1FteTRl2NOcE7isGM2svZ4e8IlZ3Kef0SEUwoSg CW5XP+cbHa/lDxzvc1pmkuSqJs/kPL5T80PJljzfx8mci54cXUtYMDVgyqEVg7InVxYq WJj4Z+bHPzPsoZfzXYvYHSTv0suEXhiOvqAPUPCZCwz856G4dG0pFNdtQt6KLuS7l0Fp rt66K70YcIaUiH06+nFNaISFH0hmE8DHxkrMZ1zTaVemyfBchG4ZakTE3RMMziPReZ9T kd5tTCNZRSJFXAtKb3zbvnn6Ck6ae3quMsV5uTMgy7xBFCerM0wfBT4TCui2pOWjDb3I lsMg== 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:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=rquQ8wDF13kuQATC5YYw+dEo0yOaMMMchDgnI+iOso0=; b=Mu9e+ZCOVoel+IVL3aGh58++fq0BjwsFO0njpuJK3H8MoFrE5N7UteY4fWi7TwodmQ KXjlJwx6M/uqEYLDt5ftqaOw8qBWEw5RxmlNlHoNBuOr4HMvxcvv4KDpCGWdDPb2nbUS +MRmdAhTfATdg2Ak/9xfTKU1Rlw33+giaTLnh71nmSUxP1bFPBW9iN7GJ0jyVeCvbquX CHoiQhtvjHEHIUca4iNzoVrA68j59bmN0g6+2Bz7+ou3IdqgxlCtiMyS42WKBeYh119v QvcBcUYiECqlrDfZhX92ueCf5QKvRY+nFWjbnPoFTUU0rbHh1XQ4E2HyCAaSNlsIlJZ1 lT6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=AzTa+G5R; 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 i19si2756805ejg.457.2020.08.27.20.20.48; Thu, 27 Aug 2020 20:21:11 -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=AzTa+G5R; 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 S1728251AbgH1DTs (ORCPT + 99 others); Thu, 27 Aug 2020 23:19:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727817AbgH1DTr (ORCPT ); Thu, 27 Aug 2020 23:19:47 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44883C061264 for ; Thu, 27 Aug 2020 20:19:47 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id w186so4792310pgb.8 for ; Thu, 27 Aug 2020 20:19:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=rquQ8wDF13kuQATC5YYw+dEo0yOaMMMchDgnI+iOso0=; b=AzTa+G5R6IdGurzzZWnNFoLZm2hiJ9IgYqhvoL0VcGsg7QxxeNMbbfyaHVacCzgXYF W+XXTMpllFfQIHsttRKcFToWcwilx2XHDyw7b+wydGIaWq9ZH/8zh56Sy+Kfuf6zauhi alo5eLMVmqYYoVG5qwlhfnBlGtUZJZRikJfSh2ilLIRGbRJ4/sEUMNebeC9TtDqGAU/e B1JovEd86mjoD5es4+Yd0v3CsFaroPOB4DlgKX4gyMVrIcwcOajsv2BKl+OB56z9FmoG pv9zEPwSY99cmu0/Uh5sgZzVy9jzKXDEBs/UT1q66lq97IKN3/fwsMsZBbCdVTJ+aFQD xUng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=rquQ8wDF13kuQATC5YYw+dEo0yOaMMMchDgnI+iOso0=; b=hzV9ME51oD2mOwBRIpJWlM01tuIqRafYcgOq7V5qw2OkwxhiCvwOy9Xb4Z9Ho44hIT N5juGZXNO31ocRVF+DIm/XSa8j/Gxz/+L7lXq/Bw2rw4d6VzUFFNzq9CmKGes2By7cbD Iw3BIDQy8SESurmDLYwwCTFe7sqEs9bdvjq72qGfQZdm57Xj92TqvLEz5xgD8MspfKZ3 ExN220KXCLrqXFsELHqtUf2gq+5m9zx5Q47sM98c5lxaHyB9+fAxiuneA4AcPZ/2xALk E/NNoeoJme4YqTwyqEhHoo8l91TKaSOc9L/3RFkCoMyKS2e6wa6Zou8sYBVzIYZyeFZn qhtQ== X-Gm-Message-State: AOAM53335vhmJXo76twJA17zJwhYrToJh3SYUFTRp31z7K0I4XfiE3eW fTr+GVZuh57GMHSdjofX/QiOrc5u0xR8Sx1V X-Received: by 2002:a62:a203:: with SMTP id m3mr19057061pff.86.1598584786857; Thu, 27 Aug 2020 20:19:46 -0700 (PDT) Received: from Smcdef-MBP.local.net ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 135sm4475756pfy.61.2020.08.27.20.19.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Aug 2020 20:19:46 -0700 (PDT) From: Muchun Song To: keescook@chromium.org, mhiramat@kernel.org, rostedt@goodmis.org, alex.popov@linux.com, miguel.ojeda.sandonis@gmail.com Cc: linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH v2] stackleak: Fix a race between stack erasing sysctl handlers Date: Fri, 28 Aug 2020 11:19:28 +0800 Message-Id: <20200828031928.43584-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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