Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp42013pxu; Thu, 3 Dec 2020 18:49:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJxL9+aNZPwZL4s6UkwXlpVwCDM31zhFMyb2Eita58p5kJielAedqm3PMdDHaU3Dm7bHCdbY X-Received: by 2002:a17:906:a415:: with SMTP id l21mr5133033ejz.2.1607050182070; Thu, 03 Dec 2020 18:49:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607050182; cv=none; d=google.com; s=arc-20160816; b=Em4kSeClI4Bf9g3Vf2y3JE9H+GUR0xq9/5TpnD1QmFohXVCMCfL+QtSkXIdVsQJ/FQ ncF+TiXdIFhGA0MKLZ/FkklsV3RdCUBEw+nkQ4jloOYAKyuF+/gl2x5JmYm4zyowpujY zV/aAkEZ2Cv9HBHCApyrAFXOD+EeZNYwxOr7Bv03VulUrJpT7wOrtckG4uHVKJRP7IKG pRWLVFLgSOhfXh8P73II8oghLB3WOqWBKvgZmxSMh6DEMENp5UBHe8uqHBnVsEslCPFt OhrssnzbALaJ8j/RBIiaryG1ZihZQlvSQ7PNMeqrDVNBSQetgUfxSM5qgpj8mk9mHYNM zD3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=cNdhDj5crZM5qaB5oEOBJ9YjJvr1U2iWZIfetNSw16Y=; b=qUHHDJbhI1d8m+DohK0NnQG2IFZ6obo7dTfWTzgFwLXNMB8L1vBcsYRVCGgCvwCXJv UY6GvRfIs9giWV1+wBT7nUCAhNLla1BD4/uLVIkaiYQUQuDm05h9Of/LT8K1KhqT7I2u 0fdNJUCrM2LV11OYOAUZJJuSq1M8k6u7i3IVKMr2wtMnOD/b/TxgW7CSz28RNx340fFx f1MbXBJ0jnAesR0QRaWtvu9EImZHftQw935UHrK9t4jW03s5lYN3Q85326jb87JPVG5N qS48JKKQoPHOjPTHdIdyZQQakwCOQldfqQvJxEzsDRUxCkFvyLnxbAA5AKQXk4+KcaTE qHUg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l28si2050019edj.68.2020.12.03.18.48.55; Thu, 03 Dec 2020 18:49:42 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727240AbgLDCof (ORCPT + 99 others); Thu, 3 Dec 2020 21:44:35 -0500 Received: from mailgw01.mediatek.com ([210.61.82.183]:41575 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726112AbgLDCof (ORCPT ); Thu, 3 Dec 2020 21:44:35 -0500 X-UUID: 47ccc151455948f492b256b182df520b-20201204 X-UUID: 47ccc151455948f492b256b182df520b-20201204 Received: from mtkcas11.mediatek.inc [(172.21.101.40)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.14 Build 0819 with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1007854576; Fri, 04 Dec 2020 10:43:50 +0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkmbs08n1.mediatek.inc (172.21.101.55) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 4 Dec 2020 10:43:48 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas11.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Fri, 4 Dec 2020 10:43:50 +0800 From: Miles Chen To: Alexey Dobriyan , Andrew Morton CC: , , , , Miles Chen , Andrey Konovalov , Alexander Potapenko , Vincenzo Frascino , Andrey Ryabinin , Catalin Marinas , Dmitry Vyukov , Marco Elver , Will Deacon , "Eric W . Biederman" , Song Bao Hua , Subject: [PATCH v3] proc: use untagged_addr() for pagemap_read addresses Date: Fri, 4 Dec 2020 10:43:47 +0800 Message-ID: <20201204024347.8295-1-miles.chen@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When we try to visit the pagemap of a tagged userspace pointer, we find that the start_vaddr is not correct because of the tag. To fix it, we should untag the userspace pointers in pagemap_read(). I tested with 5.10-rc4 and the issue remains. Explanation from Catalin in [1]: :Arguably, that's a user-space bug since tagged file offsets were never :supported. In this case it's not even a tag at bit 56 as per the arm64 :tagged address ABI but rather down to bit 47. You could say that the :problem is caused by the C library (malloc()) or whoever created the :tagged vaddr and passed it to this function. It's not a kernel :regression as we've never supported it. : :Now, pagemap is a special case where the offset is usually not generated :as a classic file offset but rather derived by shifting a user virtual :address. I guess we can make a concession for pagemap (only) and allow :such offset with the tag at bit (56 - PAGE_SHIFT + 3). My test code is based on [2]: A userspace pointer which has been tagged by 0xb4: 0xb400007662f541c8 === userspace program === uint64 OsLayer::VirtualToPhysical(void *vaddr) { uint64 frame, paddr, pfnmask, pagemask; int pagesize = sysconf(_SC_PAGESIZE); off64_t off = ((uintptr_t)vaddr) / pagesize * 8; // off = 0xb400007662f541c8 / pagesize * 8 = 0x5a00003b317aa0 int fd = open(kPagemapPath, O_RDONLY); ... if (lseek64(fd, off, SEEK_SET) != off || read(fd, &frame, 8) != 8) { int err = errno; string errtxt = ErrorString(err); if (fd >= 0) close(fd); return 0; } ... } === kernel fs/proc/task_mmu.c === static ssize_t pagemap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { ... src = *ppos; svpfn = src / PM_ENTRY_BYTES; // svpfn == 0xb400007662f54 start_vaddr = svpfn << PAGE_SHIFT; // start_vaddr == 0xb400007662f54000 end_vaddr = mm->task_size; /* watch out for wraparound */ // svpfn == 0xb400007662f54 // (mm->task_size >> PAGE) == 0x8000000 if (svpfn > mm->task_size >> PAGE_SHIFT) // the condition is true because of the tag 0xb4 start_vaddr = end_vaddr; ret = 0; while (count && (start_vaddr < end_vaddr)) { // we cannot visit correct entry because start_vaddr is set to end_vaddr int len; unsigned long end; ... } ... } [1] https://lore.kernel.org/patchwork/patch/1343258/ [2] https://github.com/stressapptest/stressapptest/blob/master/src/os.cc#L158 Cc: Andrew Morton Cc: Alexey Dobriyan Cc: Andrey Konovalov Cc: Alexander Potapenko Cc: Vincenzo Frascino Cc: Andrey Ryabinin Cc: Catalin Marinas Cc: Dmitry Vyukov Cc: Marco Elver Cc: Will Deacon Cc: Eric W. Biederman Cc: Song Bao Hua (Barry Song) Cc: stable@vger.kernel.org # v5.4- Signed-off-by: Miles Chen Reviewed-by: Vincenzo Frascino Reviewed-by: Catalin Marinas --- Change since v1: 1. Follow Eirc's and Catalin's suggestion to avoid overflow 2. Cc to stable v5.4- 3. add explaination from Catalin to the commit message Change since v2: 1. replace less-than with less-than or equal 2. Fix bad spelling in commit message 3. Fix will's email address --- fs/proc/task_mmu.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 217aa2705d5d..ee5a235b3056 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1599,11 +1599,15 @@ static ssize_t pagemap_read(struct file *file, char __user *buf, src = *ppos; svpfn = src / PM_ENTRY_BYTES; - start_vaddr = svpfn << PAGE_SHIFT; end_vaddr = mm->task_size; /* watch out for wraparound */ - if (svpfn > mm->task_size >> PAGE_SHIFT) + start_vaddr = end_vaddr; + if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) + start_vaddr = untagged_addr(svpfn << PAGE_SHIFT); + + /* Ensure the address is inside the task */ + if (start_vaddr > mm->task_size) start_vaddr = end_vaddr; /* -- 2.18.0