Received: by 10.213.65.68 with SMTP id h4csp2116007imn; Sun, 8 Apr 2018 20:06:49 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/2kpTstKpminUKql0PbM/lqmH14EtfGWn1IIRu4dF7SVVpRddoMWUbGjpcYFpU/qUXMf68 X-Received: by 2002:a17:902:67c5:: with SMTP id g5-v6mr3823324pln.93.1523243209952; Sun, 08 Apr 2018 20:06:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523243209; cv=none; d=google.com; s=arc-20160816; b=m6GQICA3n8dPqEynnx0hI7S79Eehjngt7KLxBR78PrXTQ5D19M0/2VeOqmDfRbAH4e 1sPg2tk31X4vY8K2zprNU5AmLEIM/LHRZ0F8co283TXiPHSmNQ51qnSoXLwT5MLLmKpK 8lj8R+24WD8xdLhrE/BhMFJkjISKQrl66rb4q+RYGI8y88kOKOCj1NvW47Of3ug+Qi+F McFbRZ9iZL4EzjlzUef/CfH/sRtpg233OuVfwusmehm4wpj6DsjyZY7wn+Izmr24as7J lvs9WsWxh3J4htBDIG7AOm1pofL8GvFlcOKDE+j5fWIcDxxMOighae4hIF+g14D0hAzf lIBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=3gCEeCyGegXuwDbDBNM6c502Rkqhktz93vLWoXnOvoo=; b=fBRF6qvV017Et2/+xQ3mco+ZcxysXP2mRozlXLDU3JWCMr3b0TxKO4h282ta3gZGUU cIOqVkBSJFW+hLnhz3tO/WVZA9MHkIkdbWOp9AAzDSudba2QSeLx69bukruPLG2raF5S ZROZ33AHoHFouwLt6b4N/Ffg7zZLBCm6OJJZMv5xEWLUZDZosUnUMLlQWI5Fi/U+k77I AbCxCMq06j1dgpywOuDy07GahOV5NX2kSND7PxSvC2yPyEzZ03PwbGiHCP8EuL9PbwiQ JT2/222FEkiUnIvC5dPyTE9gvpo0Z1wSsAFqbv257I5pQs3bUkAmsH9z7hjhrBDueY/e OhKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=GzigFVYM; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 4-v6si13617335plb.205.2018.04.08.20.06.12; Sun, 08 Apr 2018 20:06:49 -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=@microsoft.com header.s=selector1 header.b=GzigFVYM; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757074AbeDIBJv (ORCPT + 99 others); Sun, 8 Apr 2018 21:09:51 -0400 Received: from mail-co1nam03on0129.outbound.protection.outlook.com ([104.47.40.129]:43760 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933017AbeDIAiq (ORCPT ); Sun, 8 Apr 2018 20:38:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=3gCEeCyGegXuwDbDBNM6c502Rkqhktz93vLWoXnOvoo=; b=GzigFVYMZWtpnlrmNuJAXXA04vvDPuGtmqQFnIkDXz05OI9YNIHnJnsdLaC/Dum9MR2XqaCwuw3G3V5IfcIs7utLmbetf2Obm4yCdKoLbOtSMBf2IEh0zY0VIhWeukVFMw2GdhJK/F++gNBBfv70+7H7NTNH17tvvGm2gmqjYko= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB1095.namprd21.prod.outlook.com (52.132.130.17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.696.0; Mon, 9 Apr 2018 00:38:41 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059%2]) with mapi id 15.20.0696.003; Mon, 9 Apr 2018 00:38:41 +0000 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Yisheng Xie , Andi Kleen , Chris Salls , Christopher Lameter , David Rientjes , Ingo Molnar , Naoya Horiguchi , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH AUTOSEL for 4.4 139/162] mm/mempolicy: fix the check of nodemask from user Thread-Topic: [PATCH AUTOSEL for 4.4 139/162] mm/mempolicy: fix the check of nodemask from user Thread-Index: AQHTz5nWMvRV/KLzmE2mcTcgbm3EWQ== Date: Mon, 9 Apr 2018 00:29:35 +0000 Message-ID: <20180409002738.163941-139-alexander.levin@microsoft.com> References: <20180409002738.163941-1-alexander.levin@microsoft.com> In-Reply-To: <20180409002738.163941-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB1095;7:1IB1wkoSkHIGAFvFFPzuWtFCz7TZgwK1lPABFHhKRpTliNx98lM3tMfz9k/7Xx2li3xGSRF3C0CVqIiVlp9cODRvlpcZM7cqXBzSHqKEu6yFcRMfEXh43BDhYtebQH2bAJYcug8RcEFoKAsUgE+S2ZRCTF4PMgEpLYlN1LDOhMPjpdRkwyoHlybX316v/XfLu76ZAS+iT9OWz89lu5czbTMH2bsYdhl8i4/i+KLgpa+7EEdX+QGGnRW01psvGW18;20:Bi2S7F+LOqksyyrzXnIX6M4GXyPBby/bhajQjTtorfO0KojjK2kNzBvcQKFxncdfREGwWQTFTO5LpZ29ElCIeKvJyI+feGlP3v9ovHu+3uvbpZ98olBbZxzohccl1UvnpzwTYrYB+cX2c/6AwGOcwuFc8Qt4BgOmq0XLj6ZGMao= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: bad089f2-a3b3-4ed5-adf9-08d59db23de2 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB1095; x-ms-traffictypediagnostic: DM5PR2101MB1095: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(50582790962513)(211936372134217)(42068640409301)(153496737603132)(228905959029699); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(8121501046)(5005006)(3231221)(944501327)(52105095)(10201501046)(3002001)(93006095)(93001095)(6055026)(61426038)(61427038)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123560045)(20161123562045)(20161123564045)(6072148)(201708071742011);SRVR:DM5PR2101MB1095;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB1095; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39380400002)(346002)(366004)(396003)(39860400002)(376002)(189003)(199004)(26005)(10090500001)(476003)(105586002)(86362001)(53376002)(107886003)(11346002)(446003)(2616005)(102836004)(6306002)(76176011)(6512007)(97736004)(36756003)(6666003)(6506007)(8676002)(59450400001)(99286004)(4326008)(10290500003)(81156014)(86612001)(81166006)(186003)(486006)(2900100001)(106356001)(1076002)(72206003)(478600001)(305945005)(8936002)(25786009)(3660700001)(7736002)(966005)(68736007)(66066001)(110136005)(316002)(54906003)(7416002)(3280700002)(2906002)(3846002)(6116002)(6436002)(53936002)(6486002)(22452003)(2501003)(5250100002)(14454004)(5660300001)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB1095;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: ATo30K5BnosuaoQ27mPKBqdYCnSDjs7p5JysrpefhpaQ+sTn9PIKH4vHgvcuB8XnMhgVtkhfM9UN38hNhaGOjjOSuz7tDj8c4BxbFlVKXhoUMhdjVRY/f1waP2bNQ4xuH+90BZDlkfZFri+88HS69QujhMvVG1h7xOmPPcTU2myL1C0yqD7yqF5pkdEcyM5SHj0fMOfbDqrGngW5OAteRdphGcsarGKEyKaJ2VBBPXIAk0F7AyVr+wZWf7X7gTvOdAkK7KfjEdRo7TRnXPmfImauf9RoZsGxAx6/0SobwgO7/9kSxA65nuLHA83D1vpVkXwapKRBMngWM/dSPOdpyRYbMM/s8SI+/t0kFnwGQNcw4oU4ARLkeAgiQ7uOgA19lejn31Sb2V+PR99+4LEswG8buBAe1lM+9VzobI6NEP0= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: bad089f2-a3b3-4ed5-adf9-08d59db23de2 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:29:35.2713 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB1095 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yisheng Xie [ Upstream commit 56521e7a02b7b84a5e72691a1fb15570e6055545 ] As Xiaojun reported the ltp of migrate_pages01 will fail on arm64 system which has 4 nodes[0...3], all have memory and CONFIG_NODES_SHIFT=3D2: migrate_pages01 0 TINFO : test_invalid_nodes migrate_pages01 14 TFAIL : migrate_pages_common.c:45: unexpected fai= lure - returned value =3D 0, expected: -1 migrate_pages01 15 TFAIL : migrate_pages_common.c:55: call succeeded= unexpectedly In this case the test_invalid_nodes of migrate_pages01 will call: SYSC_migrate_pages as: migrate_pages(0, , {0x0000000000000001}, 64, , {0x0000000000000010}, 64) = =3D 0 The new nodes specifies one or more node IDs that are greater than the maximum supported node ID, however, the errno is not set to EINVAL as expected. As man pages of set_mempolicy[1], mbind[2], and migrate_pages[3] mentioned, when nodemask specifies one or more node IDs that are greater than the maximum supported node ID, the errno should set to EINVAL. However, get_nodes only check whether the part of bits [BITS_PER_LONG*BITS_TO_LONGS(MAX_NUMNODES), maxnode) is zero or not, and remain [MAX_NUMNODES, BITS_PER_LONG*BITS_TO_LONGS(MAX_NUMNODES) unchecked. This patch is to check the bits of [MAX_NUMNODES, maxnode) in get_nodes to let migrate_pages set the errno to EINVAL when nodemask specifies one or more node IDs that are greater than the maximum supported node ID, which follows the manpage's guide. [1] http://man7.org/linux/man-pages/man2/set_mempolicy.2.html [2] http://man7.org/linux/man-pages/man2/mbind.2.html [3] http://man7.org/linux/man-pages/man2/migrate_pages.2.html Link: http://lkml.kernel.org/r/1510882624-44342-3-git-send-email-xieyisheng= 1@huawei.com Signed-off-by: Yisheng Xie Reported-by: Tan Xiaojun Acked-by: Vlastimil Babka Cc: Andi Kleen Cc: Chris Salls Cc: Christopher Lameter Cc: David Rientjes Cc: Ingo Molnar Cc: Naoya Horiguchi Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/mempolicy.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index c947014d128a..6196505a1119 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1232,6 +1232,7 @@ static int get_nodes(nodemask_t *nodes, const unsigne= d long __user *nmask, unsigned long maxnode) { unsigned long k; + unsigned long t; unsigned long nlongs; unsigned long endmask; =20 @@ -1248,13 +1249,19 @@ static int get_nodes(nodemask_t *nodes, const unsig= ned long __user *nmask, else endmask =3D (1UL << (maxnode % BITS_PER_LONG)) - 1; =20 - /* When the user specified more nodes than supported just check - if the non supported part is all zero. */ + /* + * When the user specified more nodes than supported just check + * if the non supported part is all zero. + * + * If maxnode have more longs than MAX_NUMNODES, check + * the bits in that area first. And then go through to + * check the rest bits which equal or bigger than MAX_NUMNODES. + * Otherwise, just check bits [MAX_NUMNODES, maxnode). + */ if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) { if (nlongs > PAGE_SIZE/sizeof(long)) return -EINVAL; for (k =3D BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) { - unsigned long t; if (get_user(t, nmask + k)) return -EFAULT; if (k =3D=3D nlongs - 1) { @@ -1267,6 +1274,16 @@ static int get_nodes(nodemask_t *nodes, const unsign= ed long __user *nmask, endmask =3D ~0UL; } =20 + if (maxnode > MAX_NUMNODES && MAX_NUMNODES % BITS_PER_LONG !=3D 0) { + unsigned long valid_mask =3D endmask; + + valid_mask &=3D ~((1UL << (MAX_NUMNODES % BITS_PER_LONG)) - 1); + if (get_user(t, nmask + nlongs - 1)) + return -EFAULT; + if (t & valid_mask) + return -EINVAL; + } + if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long= ))) return -EFAULT; nodes_addr(*nodes)[nlongs-1] &=3D endmask; --=20 2.15.1