Received: by 2002:a05:6520:1682:b0:147:d1a0:b502 with SMTP id ck2csp5596920lkb; Mon, 11 Oct 2021 09:39:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzI4aigLgBmlecB8vsoHebhzK+xhqgSihiluWk+s0xmow27XX1pv4Ex9J3+Ez2oD0iSpV5j X-Received: by 2002:a17:90b:4b48:: with SMTP id mi8mr52895pjb.26.1633970369163; Mon, 11 Oct 2021 09:39:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633970369; cv=none; d=google.com; s=arc-20160816; b=pJuwi7N3PqHvmEwDM91TxRG/sV61yK5C68QHVYfh/+Yr5w/6j6u0P1dwtRnMKmzZ8I 6cr3SBpfXMqim9ju7DFXWvnimpGnxGpQdezPOwn7lpcPz5cxbqhayt/xvZsfjb1kQEmh 6IOfwnGEwxUcjvyaWW1J064bSfy+zXJwmkXzLdr9BUI6Vr6AaZNWi+pyERTyGHklApJg jAlZcDf3iHzdFtftTGyUAZnHZn8q+9moH45+c2iS2jrk3GAXP7HnYO367fKgvRV5j4ku no2knnOg1PEDBqhnTkEj3XUXG6xoYb2VbDrwIYIiLHT1+AQOd0t2ZRkjxyi0+T1NQF9W EC3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=PGXIxeEHVsHqYx+tvGQYyqw86qWLKw3qR1J0Qthd5AE=; b=kGH2thWe8L9BPj4tgqY2Niy6vLdpRB7XzfNFKax9JdcmgvZDZCcW7HcCLqNvbEHaPg TQ86fMZjvkflZlcgP0Q4O1Tc9LUi7SSWA6/Qni5JTTilq+k+pu1+Q4uNB4+mLKCG00jo swNBi0hIgtFaR+ogMuKCB4IuVXBqrqc17yG0zjtyrnbxpQ0V9cK9kjAJAYxIKRtknB6t zcz2YaxjZ5uRzcyCPMfL7rJBbpGIXxu1xJ+IidGl7ub13s5rVNCbE5dp/9NIvvxMoPe2 HnmTHaLcax4kEX5zbBBkNAIVJoS7E4gAO2/PXrQiFzcC/NQvvchW75O2+nRW0L1RX3bv GpoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@me.com header.s=1a1hai header.b="WBP/LG1p"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=me.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j8si787063pgb.348.2021.10.11.09.39.17; Mon, 11 Oct 2021 09:39:29 -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=@me.com header.s=1a1hai header.b="WBP/LG1p"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=me.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238759AbhJKOfk (ORCPT + 99 others); Mon, 11 Oct 2021 10:35:40 -0400 Received: from pv50p00im-ztdg10021201.me.com ([17.58.6.45]:51210 "EHLO pv50p00im-ztdg10021201.me.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238509AbhJKOfi (ORCPT ); Mon, 11 Oct 2021 10:35:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=me.com; s=1a1hai; t=1633962817; bh=PGXIxeEHVsHqYx+tvGQYyqw86qWLKw3qR1J0Qthd5AE=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=WBP/LG1p0SUBxAfQpbsq6XYPrMDSh2dqVw2vOcNnRhDlSEyWnxojnYY9IfeT3640t xMrDVflkyDb5D6qIx5MDA79RotxxGYeaxQWjO5puNgOyiWJaRAt77oMM7mEn/Z2omD //+WAlZwcANrW2KCTPhhrb4I1UetrzaHVaesXlbwN7C8cUcOTdqX4GtuBu7RYqu4ZE /LslAXkfqxYWNqyYoAe0Y3KP6Kyp6OpU1qw3M+DyziZeRvY3tG9VvFBvjp+WEi3Uax zW3xH8QK0J3muzRVNNSm2HP8PygSnBXHR4kvSDGb6B4BTi0gC1/n8/h00bmDUaHY51 E73BZiVKDglTQ== Received: from xiongwei.. (unknown [120.245.2.114]) by pv50p00im-ztdg10021201.me.com (Postfix) with ESMTPSA id 7A23CA405D9; Mon, 11 Oct 2021 14:33:29 +0000 (UTC) From: sxwjean@me.com To: linux-mm@kvack.org, x86@kernel.org Cc: Xiongwei Song , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Kees Cook , "H. Peter Anvin" , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Andrew Morton , Arnd Bergmann , Al Viro , Gabriel Krisman Bertazi , Lai Jiangshan , Huang Rui , Yazen Ghannam , Kim Phillips , Oleg Nesterov , Balbir Singh , "David S. Miller" , sxwjean@me.com, linux-kernel@vger.kernel.org Subject: [PATCH v2 0/6] Use generic code for randomization of virtual address of x86 Date: Mon, 11 Oct 2021 22:31:44 +0800 Message-Id: <20211011143150.318239-1-sxwjean@me.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.790 definitions=2021-10-11_05:2021-10-07,2021-10-11 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-2009150000 definitions=main-2110110085 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xiongwei Song Hello, This patchset are to use generic code for randomization of virtual address of x86. Since the basic code logic of x86 is same as generic code, so no need to implement these functions on x86. Patch 1~3 are prepared to change the generic code to apply to x86. Patch 4 is to switch to generic arch_pick_mmap_layout() with ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT enabled. Also provided basically test and the result was put in commit message too. Patch 5~6 are used to handle the legacy things. Test programs(to verify if the entropy of return value of mmap is kept after applying the patchset): - C code for mmap test: #include #include #include int main(int argc, char *argv[]) { unsigned long *addr; addr = mmap(NULL, 4096, PROT_READ, MAP_SHARED|MAP_ANONYMOUS, -1, 0); if (addr == MAP_FAILED) { printf("NULL\n"); } else { printf("%lx\n", (unsigned long)addr); munmap(addr, 4096); } return 0; } - Shell script for collecting output of C progarm above and give a statistics: #!/bin/bash declare -a COUNT if [ "$1" == "" ]; then echo "Please give a test number!" exit 1 fi number=$1 for ((i=0; i<$number; i++)) do addr=$(mmaptest) addr=$(((16#$addr&0xf000000000)>>36)) COUNT[$addr]=$((COUNT[$addr]+1)) done echo " Virtual Address Range | hit times " echo "----------------------------------------" for ((i=0; i<16; i++)) do j=`echo "obase=16; $i" | bc` echo "0x7f${j,,}000000000 - 0x7f${j,,}ffffff000 | ${COUNT[i]}" done Run 10 thousands times C progam, collect the output with shell script, get the test results below: Before the patchset: Virtual Address Range | hit times ---------------------------------------- 0x7f0000000000 - 0x7f0ffffff000 | 655 0x7f1000000000 - 0x7f1ffffff000 | 617 0x7f2000000000 - 0x7f2ffffff000 | 636 0x7f3000000000 - 0x7f3ffffff000 | 625 0x7f4000000000 - 0x7f4ffffff000 | 651 0x7f5000000000 - 0x7f5ffffff000 | 591 0x7f6000000000 - 0x7f6ffffff000 | 623 0x7f7000000000 - 0x7f7ffffff000 | 627 0x7f8000000000 - 0x7f8ffffff000 | 638 0x7f9000000000 - 0x7f9ffffff000 | 586 0x7fa000000000 - 0x7faffffff000 | 637 0x7fb000000000 - 0x7fbffffff000 | 607 0x7fc000000000 - 0x7fcffffff000 | 618 0x7fd000000000 - 0x7fdffffff000 | 656 0x7fe000000000 - 0x7feffffff000 | 614 0x7ff000000000 - 0x7ffffffff000 | 619 After the patchset: Virtual Address Range | hit times ---------------------------------------- 0x7f0000000000 - 0x7f0ffffff000 | 661 0x7f1000000000 - 0x7f1ffffff000 | 645 0x7f2000000000 - 0x7f2ffffff000 | 609 0x7f3000000000 - 0x7f3ffffff000 | 594 0x7f4000000000 - 0x7f4ffffff000 | 616 0x7f5000000000 - 0x7f5ffffff000 | 622 0x7f6000000000 - 0x7f6ffffff000 | 617 0x7f7000000000 - 0x7f7ffffff000 | 582 0x7f8000000000 - 0x7f8ffffff000 | 618 0x7f9000000000 - 0x7f9ffffff000 | 629 0x7fa000000000 - 0x7faffffff000 | 635 0x7fb000000000 - 0x7fbffffff000 | 625 0x7fc000000000 - 0x7fcffffff000 | 614 0x7fd000000000 - 0x7fdffffff000 | 610 0x7fe000000000 - 0x7feffffff000 | 648 0x7ff000000000 - 0x7ffffffff000 | 675 v1 -> v2: - Spilt the patch 2 of v1 as Kees suggested. - Drop patch 1 of v1, which renamed TIF_ADDR32 to TIF_32BIT, which is unreasonable for x86. Because in x86, 64bit process can call 32bit syscall. Thanks Peterz for pointing this out. v1: - https://lkml.org/lkml/2021/9/21/482 - https://lkml.org/lkml/2021/9/21/484 - https://lkml.org/lkml/2021/9/27/688 Please review. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Kees Cook Cc: "H. Peter Anvin" Cc: Dave Hansen Cc: Andy Lutomirski Cc: Peter Zijlstra Cc: Andrew Morton Cc: Arnd Bergmann Cc: Al Viro Cc: Gabriel Krisman Bertazi Cc: Lai Jiangshan Cc: Huang Rui Cc: Yazen Ghannam Cc: Kim Phillips Cc: Oleg Nesterov Cc: Balbir Singh Cc: "David S. Miller" Cc: sxwjean@me.com Cc: linux-kernel@vger.kernel.org Xiongwei Song (6): mm/util: Assign a meaningful value to mmap_legacy_base mm/util: Allow to pass a specific task size when getting mmapping base mm/util: Support CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES x86/mm: Randomize VA with generit arch_pick_mmap_layout() x86/mm: Discard the defination of HAVE_ARCH_PICK_MMAP_LAYOUT x86/elf: Discard ARCH_HAS_ELF_RANDOMIZE selection arch/x86/Kconfig | 2 +- arch/x86/include/asm/compat.h | 5 ++ arch/x86/include/asm/processor.h | 5 +- arch/x86/mm/mmap.c | 112 ------------------------------- mm/util.c | 35 +++++++--- 5 files changed, 37 insertions(+), 122 deletions(-) -- 2.30.2