Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp238741yba; Mon, 20 May 2019 07:57:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqyITkhaGu9D8ofxc3oKfEv7yVSNxryKLaRGGdqusPRgh9YFQOgNXxXGSjIuhRaCRoEsLtZD X-Received: by 2002:a63:c601:: with SMTP id w1mr76110563pgg.190.1558364257430; Mon, 20 May 2019 07:57:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558364257; cv=none; d=google.com; s=arc-20160816; b=Ib4ex50X2KkFzbM723sYWNjl0AhZBA4/Cyso7hmaiipGmY6gD95R5E3q0VCF0fWg80 gcpstwmCkB6N5PIVSf9VnzmQE0Pvx5OAUADuG1I4+JlnY568evMCMjRB3y76UpwqcFzU W/S+WsQ4V2rGj9EZonnlkNCig9XAqgudSrMdp0re6TxAUBZeuoYXI2ouxixqcu0GVa9T KjjY4E9HC/vcoCf8BfaowPR4oGc90Pyo1W++MCjl+YxRoWqL8tgFbz9hS+WIIg3V9Ip9 eZN++Zh/s7u2cwEwA6rvZGEfPh2n5e9YivYVgzL/RISPic3/WLFFRdY4LqAUnIjA3iwO KNlg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=CCv+PrlvxyYrWPKP3AC9sdm9ccZmc/r0qxypHm0tsno=; b=dyurdJLbbg8kdcZNKM9qkEkax82cNqXqMzrWhM64zYEVyh/UEMvh13qyQML6De1WLC EOYv+VP/iCk3tq260u9OBe2aDMkkS0IPSIQeT1FV7DwvLD99rRivCoUgMVm94aJF3D+B oaK6r8vaAjU0qQf7IP3W3b1CeIGXnUv8cfKTs59LVglXJWg4CRzWjElbvyxbtjw3tTZT W6V5tb5wHe3DFBcJaP9OoRIzuL+Fghl5hyGalTn/ppMEc+eiTK0zVofsr0E7qxHnakqX eHgyVDigQw9GuOwUUnpeHdyqW85Jowu/ENTMU+R6TV5WqLGYtBds04imKUcbse9lvv/n GNew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VPbZY09T; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k5si12494261pfi.16.2019.05.20.07.57.22; Mon, 20 May 2019 07:57:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VPbZY09T; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387816AbfETMTY (ORCPT + 99 others); Mon, 20 May 2019 08:19:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:60598 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387799AbfETMTU (ORCPT ); Mon, 20 May 2019 08:19:20 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 95079208C3; Mon, 20 May 2019 12:19:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558354759; bh=FnOiAWCxbxKZVe4E+ZcIN19Am0EYnQ0noptC7Y+yIqM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VPbZY09TVnSmrhc6aH244GWjaUKQ2Myib2Yn3zRIs+OphyhpgcWon43tpK5DzSnw9 yrCXeJ2O/dQCG0AqSdBu5TIpbYm+MY14NsLpSyswdHcEozv7GY1AMVHrSalJ3zYvHr 6zwp8YriDCeRI1hZB0i5dK0usxg2Ao+Ll3e4h6gY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiri Kosina , Vlastimil Babka , Josh Snyder , Michal Hocko , Andy Lutomirski , Dave Chinner , Kevin Easton , Matthew Wilcox , Cyril Hrubis , Tejun Heo , "Kirill A. Shutemov" , Daniel Gruss , Andrew Morton , Linus Torvalds , Dominique Martinet Subject: [PATCH 4.14 32/63] mm/mincore.c: make mincore() more conservative Date: Mon, 20 May 2019 14:14:11 +0200 Message-Id: <20190520115234.872323738@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190520115231.137981521@linuxfoundation.org> References: <20190520115231.137981521@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiri Kosina commit 134fca9063ad4851de767d1768180e5dede9a881 upstream. The semantics of what mincore() considers to be resident is not completely clear, but Linux has always (since 2.3.52, which is when mincore() was initially done) treated it as "page is available in page cache". That's potentially a problem, as that [in]directly exposes meta-information about pagecache / memory mapping state even about memory not strictly belonging to the process executing the syscall, opening possibilities for sidechannel attacks. Change the semantics of mincore() so that it only reveals pagecache information for non-anonymous mappings that belog to files that the calling process could (if it tried to) successfully open for writing; otherwise we'd be including shared non-exclusive mappings, which - is the sidechannel - is not the usecase for mincore(), as that's primarily used for data, not (shared) text [jkosina@suse.cz: v2] Link: http://lkml.kernel.org/r/20190312141708.6652-2-vbabka@suse.cz [mhocko@suse.com: restructure can_do_mincore() conditions] Link: http://lkml.kernel.org/r/nycvar.YFH.7.76.1903062342020.19912@cbobk.fhfr.pm Signed-off-by: Jiri Kosina Signed-off-by: Vlastimil Babka Acked-by: Josh Snyder Acked-by: Michal Hocko Originally-by: Linus Torvalds Originally-by: Dominique Martinet Cc: Andy Lutomirski Cc: Dave Chinner Cc: Kevin Easton Cc: Matthew Wilcox Cc: Cyril Hrubis Cc: Tejun Heo Cc: Kirill A. Shutemov Cc: Daniel Gruss Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/mincore.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) --- a/mm/mincore.c +++ b/mm/mincore.c @@ -169,6 +169,22 @@ out: return 0; } +static inline bool can_do_mincore(struct vm_area_struct *vma) +{ + if (vma_is_anonymous(vma)) + return true; + if (!vma->vm_file) + return false; + /* + * Reveal pagecache information only for non-anonymous mappings that + * correspond to the files the calling process could (if tried) open + * for writing; otherwise we'd be including shared non-exclusive + * mappings, which opens a side channel. + */ + return inode_owner_or_capable(file_inode(vma->vm_file)) || + inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0; +} + /* * Do a chunk of "sys_mincore()". We've already checked * all the arguments, we hold the mmap semaphore: we should @@ -189,8 +205,13 @@ static long do_mincore(unsigned long add vma = find_vma(current->mm, addr); if (!vma || addr < vma->vm_start) return -ENOMEM; - mincore_walk.mm = vma->vm_mm; end = min(vma->vm_end, addr + (pages << PAGE_SHIFT)); + if (!can_do_mincore(vma)) { + unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE); + memset(vec, 1, pages); + return pages; + } + mincore_walk.mm = vma->vm_mm; err = walk_page_range(addr, end, &mincore_walk); if (err < 0) return err;