Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp756070pxb; Thu, 21 Oct 2021 08:55:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwL3Hm+yCvlqxi3t2rLcJvEAEnV7RyrW2Rfe3eGKGNuhunPU1OXdBTEjNwmHWHrDsUj5f3r X-Received: by 2002:a05:6402:1908:: with SMTP id e8mr8626726edz.252.1634831708171; Thu, 21 Oct 2021 08:55:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634831708; cv=none; d=google.com; s=arc-20160816; b=t0TRcCMbf+EzvVOZL5hR6oomAj4EibCrLyUTe0wY8N+MX+4bwswD8Gv/aE2wVOU4c/ TGXyT7HsQlwLX2NK6QdsJ4tjyuDQEwxPuQqY4ollYdJ8aA4PplJrYjp6f4JSWUcDhShL l1mQDQwrOkugQJjpVoei2h8KsOReB3M7Uey/Og6NF8uQKnW3m4M3WceGLZJJuT8q4Npd MWcbRZA9Wu/7eoCqxRfpCRUWbOoGySwlU87mzfes12kZwpiZ4Hwh0Uck2ckWJ7sgoZjZ AngnSe8JrmKZayxcfV5FxLBQ56cwEMluOHVHmTHOAFA4F1I6Xz311L9TTRjmOaw4g3Y8 O3jQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=wGNuVhi5Wfv6Y5HCVEAnOz4vrOFq78Ll27lC1Wx8arY=; b=n8lh7esSpV8f0OtSr3KFaJOd3chxz0QzSTFTyKZRgwhH5D6s/7IlTxeeR41x8PYMr/ iKpdYcf8ozalPD1m5TKkEXmGypyTyuzKLIBC+G23s1f2hKbQ0t3wxh8gGtZRuueimOXW b6mEP7Sta+HbsAJo3QDWurknLE5M9hSV3rNu4e//Op8zv4Hv+JAOxgCHdxRZBPHQOBQf E8qierfs01O/l8XaCJaatp/PlcCQhBplMbT9GorLhcnFg+GFy65CtCfIVzwxl/nmcKBU vYJO4pqBLKpQaLSYXb0X2t6GZgS6WgkZxHqUmPPTTNWM5ZoX2QWMdIZrx0yq7bVeIHXd Rxxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gAn1elUn; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b18si7991796ejh.638.2021.10.21.08.54.44; Thu, 21 Oct 2021 08:55:08 -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=@redhat.com header.s=mimecast20190719 header.b=gAn1elUn; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231758AbhJUPwn (ORCPT + 99 others); Thu, 21 Oct 2021 11:52:43 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33888 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230280AbhJUPwg (ORCPT ); Thu, 21 Oct 2021 11:52:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634831420; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wGNuVhi5Wfv6Y5HCVEAnOz4vrOFq78Ll27lC1Wx8arY=; b=gAn1elUnWB0y2YXFk+ZQQZEmG0R1Jvb5GkAKkkXPBqDWlrtdHmmXk9wzPDXkAhJTt5yY2Q DOofvb4nUk2jNK80ASi7FgTOkh5j41j4MXxK/uk7fVMsONSCp0n5W2I6MufmpVH+QgRzue AbGo7B9hwJL2xiE+x26443PDQGc0+x8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-154-rM6t4gPKMfCqMzDSWlvLyA-1; Thu, 21 Oct 2021 11:50:19 -0400 X-MC-Unique: rM6t4gPKMfCqMzDSWlvLyA-1 Received: by mail-wm1-f69.google.com with SMTP id c5-20020a05600c0ac500b0030dba7cafc9so47290wmr.5 for ; Thu, 21 Oct 2021 08:50:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=wGNuVhi5Wfv6Y5HCVEAnOz4vrOFq78Ll27lC1Wx8arY=; b=rAo9HqpXUNEMjz8dWzZMwMSVb5CW6X5OoewPwMsKMHnMtk2aXxe/1ENT2WoQ3okXva IjyIhZvNw/FiyL9Ht64S93xEw2lcUHANQH48HTL3COB6UTUE2/427i9qtpAXJc47t63N i8hcpYBoHLlv+lg4C9SWY8Xg9owU97TVfOC9sGi1iQJBg2GWlYASVOmu9r2gwjDmPSGo mLpwWlqKwiyy4XBzNYHIntlKl4jzbvDS23EY3PXBCFQnPVCEOAvBk1g8VAZ4FhLCyLVx HCrdxFZazn3YVWgQ+SEEyErTqgKsewXdBRnwO2gqCZODCxH97J3y3th0Y82UZoPbR9hP gFKg== X-Gm-Message-State: AOAM5314xqSE3inGUpOdIX+cbOyk83nEmB1p03FEYglPqZB5O/X72U9T 3doyGNohIc4ApF42u9ObE+DYJOd0nZhrn1LQOarM4V7EQigMMzm2qOhFGE/c7AIfUiDJ38/lm5q pabor2MdULbVsqFoTuzYg4+b+ X-Received: by 2002:adf:bb08:: with SMTP id r8mr8428735wrg.247.1634831417841; Thu, 21 Oct 2021 08:50:17 -0700 (PDT) X-Received: by 2002:adf:bb08:: with SMTP id r8mr8428696wrg.247.1634831417574; Thu, 21 Oct 2021 08:50:17 -0700 (PDT) Received: from [192.168.3.132] (p4ff23aba.dip0.t-ipconnect.de. [79.242.58.186]) by smtp.gmail.com with ESMTPSA id r17sm6082878wmq.47.2021.10.21.08.50.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Oct 2021 08:50:17 -0700 (PDT) Message-ID: Date: Thu, 21 Oct 2021 17:50:15 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PATCH] mm/secretmem: Avoid letting secretmem_users drop to zero Content-Language: en-US To: Kees Cook , Andrew Morton Cc: Mike Rapoport , Jordy Zomer , linux-mm@kvack.org, Dmitry Vyukov , James Bottomley , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org References: <20211021154046.880251-1-keescook@chromium.org> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20211021154046.880251-1-keescook@chromium.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 21.10.21 17:40, Kees Cook wrote: > Quoting Dmitry: "refcount_inc() needs to be done before fd_install(). > After fd_install() finishes, the fd can be used by userspace and we can > have secret data in memory before the refcount_inc(). > > A straightforward mis-use where a user will predict the returned fd > in another thread before the syscall returns and will use it to store > secret data is somewhat dubious because such a user just shoots themself > in the foot. > > But a more interesting mis-use would be to close the predicted fd and > decrement the refcount before the corresponding refcount_inc, this way > one can briefly drop the refcount to zero while there are other users > of secretmem." > > Move fd_install() after refcount_inc(). > > Cc: Mike Rapoport > Cc: Jordy Zomer > Cc: linux-mm@kvack.org > Reported-by: Dmitry Vyukov > Link: https://lore.kernel.org/lkml/CACT4Y+b1sW6-Hkn8HQYw_SsT7X3tp-CJNh2ci0wG3ZnQz9jjig@mail.gmail.com > Fixes: 9a436f8ff631 ("PM: hibernate: disable when there are active secretmem users") > Signed-off-by: Kees Cook > --- > mm/secretmem.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/mm/secretmem.c b/mm/secretmem.c > index 1fea68b8d5a6..924d84ba481f 100644 > --- a/mm/secretmem.c > +++ b/mm/secretmem.c > @@ -217,8 +217,8 @@ SYSCALL_DEFINE1(memfd_secret, unsigned int, flags) > > file->f_flags |= O_LARGEFILE; > > - fd_install(fd, file); > refcount_inc(&secretmem_users); > + fd_install(fd, file); > return fd; > > err_put_fd: > Reviewed-by: David Hildenbrand -- Thanks, David / dhildenb