Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp677067pxu; Fri, 4 Dec 2020 12:40:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJwthNZpe0xWnRcZ6UdQwat7m2+s1pbvEL0xRGrvGx8ArWzeUctbDgxycC5MOxk05DPIXNlm X-Received: by 2002:a17:906:d9c7:: with SMTP id qk7mr8836742ejb.384.1607114427675; Fri, 04 Dec 2020 12:40:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607114427; cv=none; d=google.com; s=arc-20160816; b=C1m7HN+it+JWOSmG0biwKsNl4x/KiZfPCRG3Oj2VMKQ3hX1urC2T7xy96yJincwL+5 wKnIDxB17SB8N9Lpda0/+uu2fyQo+eVWs8i6L/VrTYFL7gjpWQQhIEqVQSwxNTjyO1ar wFMfkltzCWGUpgIEnngrJR4WBr8kQEwax79CfvaOzY7QJQ6ZaHVCtsBcUgermmgZFuez 1nM3mqPovPuwOVyF9k4ymRafs/uUj/rVLkXRqSEjZ/2nsRDhtFQ322IRGAFhyQNr1Yik mYyxcJDXgoT7LNH2XyAe1V+da74vvn7e/6f04molaLbzXAyu7ONQeKNpH6FL21qRrCYY 4x1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :sender:dkim-signature; bh=AnJeit01nF61jllgCw+jNN3EZnXYnoHf3OFvBuHNiIE=; b=0HP+rX+4Kpe48GFLCpsP2a2b2toSQvVrvpmT2CuxngLmtvBdBDhQmMJ1lSLt5dKkV9 kiRw90mqyKfmXD7qzYXj7LHbiK1v0B4xqTsuhBuuoKYLqcMP3gQTFBaT8L+TwUXW6gYN +jQgwzyRzZ3eB0+M2RcqVb4sMfBr+9MYwkKl594Y7GScLQDy2HoUwCG/nXn+uHZaD8NH w9RSIfcsLS3uXVXWkBVbg/ig1aH9/vy/k54QMDqA6xAieKv7K4nYCLCbgqV/Ty4karwW jBcbb07DroaGQXKwncv8nkh0PvecyQXilClacrfTgIEmwLuotEg38HyW+/OmG2vZFd5/ yhVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Q865+I+n; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k22si4854708edj.473.2020.12.04.12.40.04; Fri, 04 Dec 2020 12:40:27 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=Q865+I+n; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729391AbgLDUgG (ORCPT + 99 others); Fri, 4 Dec 2020 15:36:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728623AbgLDUgG (ORCPT ); Fri, 4 Dec 2020 15:36:06 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7B97C061A51 for ; Fri, 4 Dec 2020 12:35:25 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id z3so8467044ybc.0 for ; Fri, 04 Dec 2020 12:35:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=AnJeit01nF61jllgCw+jNN3EZnXYnoHf3OFvBuHNiIE=; b=Q865+I+npgDxO5mvNz7nE0FFszUKiT1oj8XqaBePvExuhcuxk5iaxLW10s5HU4Lrb9 wu4fX/1igXukDWtsCu7dnMJkGlEroel7nceNG8FxOD/e666FSXT6UliuYkIM8jccC9e5 CzEvqEuM6aS8yQthD1G02jZ3bKUftXa/Ld3QT8mm0VtUHla15h/qGaDCi3Agd3s22nVW zCamj1a1vSCOZTB+v7JZBu0M/5XfPMAQQ7wK5kuzx9nsJg8jDcIROEM4BfI5gpDwM3vv Z1RFR8zSQcrSlyqPTjVr9c/M7eb1FPD90an14IUK5Vj/AAvsa7TwO06plQGM6rpUvAlP GYJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=AnJeit01nF61jllgCw+jNN3EZnXYnoHf3OFvBuHNiIE=; b=oIl2/78JvciIv8ZJO+GJtQ00c/yMrSkqx1qNf3/MyE3MJH0tJNOc/ITCBRPfHgnVcC Cg7FQUKjFN28qXbeP0OM9F4NDCf88BrV09fKlj+TF+yMgQV70n/DYMypvA8eJQgSG9Cy Us4PaTMm6O8hNtps2fGrqltqlxKyV000H2jjBAaBB7cHf17kQiXk8jgk8QUkC6n47Ezu QOIWKDzsXey8l0bfnCMPCzTyZutmf1Y+JDIsqMiIyY7EuZbNsyEXdqW84FukubXlPeh5 I6oahbheZEDJJ7uwjQm1b3ntXRmTE2te6TqPZNPDCKs1eyiI1n2AZJ5kp+S/b1ujFSNq yZLA== X-Gm-Message-State: AOAM531ncg92jOgUz4ON0c+yK+Llz1Czp/540fHqrwyIZxYrtLLbBey0 84pNljxdddlnfuZOVkW0aANUSney50oGbF2tHv1r Sender: "axelrasmussen via sendgmr" X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:f693:9fff:feef:c8f8]) (user=axelrasmussen job=sendgmr) by 2002:a25:d895:: with SMTP id p143mr503872ybg.91.1607114125017; Fri, 04 Dec 2020 12:35:25 -0800 (PST) Date: Fri, 4 Dec 2020 12:34:43 -0800 Message-Id: <20201204203443.2714693-1-axelrasmussen@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.29.2.576.ga3fc446d84-goog Subject: [PATCH] userfaultfd: selftests: fix SIGSEGV if huge mmap fails From: Axel Rasmussen To: Shuah Khan , Peter Xu , Andrew Morton , Joe Perches , Mike Rapoport Cc: Andrea Arcangeli , David Alan Gilbert , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Axel Rasmussen Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The error handling in hugetlb_allocate_area() was incorrect for the hugetlb_shared test case. Previously the behavior was: - mmap a hugetlb area - If this fails, set the pointer to NULL, and carry on - mmap an alias of the same hugetlb fd - If this fails, munmap the original area If the original mmap failed, it's likely the second one did too. If both failed, we'd blindly try to munmap a NULL pointer, causing a SIGSEGV. Instead, "goto fail" so we return before trying to mmap the alias. This issue can be hit "in real life" by forgetting to set /proc/sys/vm/nr_hugepages (leaving it at 0), and then trying to run the hugetlb_shared test. Another small improvement is, when the original mmap fails, don't just print "it failed": perror(), so we can see *why*. :) Signed-off-by: Axel Rasmussen --- tools/testing/selftests/vm/userfaultfd.c | 25 +++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 9b0912a01777..c4425597769a 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -206,19 +206,19 @@ static int hugetlb_release_pages(char *rel_area) return ret; } - static void hugetlb_allocate_area(void **alloc_area) { void *area_alias = NULL; char **alloc_area_alias; + *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE, (map_shared ? MAP_SHARED : MAP_PRIVATE) | MAP_HUGETLB, huge_fd, *alloc_area == area_src ? 0 : nr_pages * page_size); if (*alloc_area == MAP_FAILED) { - fprintf(stderr, "mmap of hugetlbfs file failed\n"); - *alloc_area = NULL; + perror("mmap of hugetlbfs file failed"); + goto fail; } if (map_shared) { @@ -227,14 +227,11 @@ static void hugetlb_allocate_area(void **alloc_area) huge_fd, *alloc_area == area_src ? 0 : nr_pages * page_size); if (area_alias == MAP_FAILED) { - if (munmap(*alloc_area, nr_pages * page_size) < 0) { - perror("hugetlb munmap"); - exit(1); - } - *alloc_area = NULL; - return; + perror("mmap of hugetlb file alias failed"); + goto fail_munmap; } } + if (*alloc_area == area_src) { huge_fd_off0 = *alloc_area; alloc_area_alias = &area_src_alias; @@ -243,6 +240,16 @@ static void hugetlb_allocate_area(void **alloc_area) } if (area_alias) *alloc_area_alias = area_alias; + + return; + +fail_munmap: + if (munmap(*alloc_area, nr_pages * page_size) < 0) { + perror("hugetlb munmap"); + exit(1); + } +fail: + *alloc_area = NULL; } static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset) -- 2.29.2.576.ga3fc446d84-goog