Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp7824740ybh; Fri, 9 Aug 2019 00:13:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqyh+0/a+LeLH2efLWUhhq6289oP50C/4gLl3tZwgJ32SAaqYftCcCvTgxuGTU0PlzPKHggP X-Received: by 2002:a65:448a:: with SMTP id l10mr15952933pgq.327.1565334821654; Fri, 09 Aug 2019 00:13:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565334821; cv=none; d=google.com; s=arc-20160816; b=gX0KqWdkY5V3agJkBmABetsMhD3Ab2hEVQjUznqng3eOCR7cYTDEC4Puibu5+RiQRD bncIpkY5oEquSzJm7CC7f0uBqGojk4ScLeVh4hxCI9g6WcNUaNKa0PiBkUOMTx71krI7 36brzqcgYBTvyOCdagdX0RtKQyIW/eEKQK4BDouafLitKKqHmxjeYVKCNkjwxcHHDbB6 xI90ox5Vbx+T2caq+/9rQqEPQtUv6oXS0jyRpnNT8x42XDDt3l/ht26Md6vYW4xwxafV MTnhVAiBWpgozgcmh9hKq2m760wWLGFKxDQXpvGhLrLwKlOKkLOwiR52sDC3gUBjj/sY pQBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from:dkim-signature; bh=Brhr0qTfy6eIZUgUAjbJm4cExvPM1CHKMDl4HPzycKU=; b=oD7a8Rlp6qga4FENz5yN141yl32ikwGi7GfdZ1MCayN0Jbt/aSLS3s+eKLJpLHdzDd PYY1WI0udjLofF7Qz52f2u6WM6/uxe6E68oeTW+B6SzjtmjluoGurp/cMzPGEDntgrkf duMcOvt5QPmi0seCVS/o19L/hlhkn/YEWbsfmN3SgrUAYh26dtJOgH87F3NUS/ASdGcq wUOxAy1UAFANdlwI/ucRYiz9pNbRsmo+54mbsA1cXGqcgcagzZtmKOSAxb3teaGffsFQ zDNVwtIICw1KN0IOasA6djqCTwXiwjrMTHuxhRLA3AttOwy40s+XeU3hKWbTfAWDQgkQ LkYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Qdl5tl0K; 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=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h5si24285256pgs.322.2019.08.09.00.13.24; Fri, 09 Aug 2019 00:13:41 -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=fail header.i=@gmail.com header.s=20161025 header.b=Qdl5tl0K; 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=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405798AbfHIHLI (ORCPT + 99 others); Fri, 9 Aug 2019 03:11:08 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:45436 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405601AbfHIHLI (ORCPT ); Fri, 9 Aug 2019 03:11:08 -0400 Received: by mail-pf1-f194.google.com with SMTP id w26so272738pfq.12 for ; Fri, 09 Aug 2019 00:11:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Brhr0qTfy6eIZUgUAjbJm4cExvPM1CHKMDl4HPzycKU=; b=Qdl5tl0KRHY+tjx50bG3jMFxwG3oW5O+XryiHiCs6NMl6IpKJ32OLhWfv0hPAzfW6c 7C04HIqzkAFTjbZPGpxwIQDlU3xHxZsIbF3KKS7/mlarPOycLZ2RWL4UeVAY1/XA1Fxv R4WCK5N53nBss2s9NUmrWz3BT5Tljk+X8VPGQ2AJ24vLwXVHyFzwE2j4WENG3yLFHy3e Zjx1lOZKGpFho+bC81eBCZrX3/GCVPwMMpfgmIn+PhtNHHnn2F2tJswHiBpaYgdybb2m 5IHp7nCGglVZYkvQsR8+rWSKNRbeKZ08M5/pvhPYJEAZ4rWPN6dNzkr7PY2xXITn1sZk x5uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Brhr0qTfy6eIZUgUAjbJm4cExvPM1CHKMDl4HPzycKU=; b=NWMeK1z+mGYxD4Yhb8+rIU03NFw6EldjqZaB9Gjtvimi9+e526AHRRyBL4DzzivHpy TPWdcexn1vhVwDtdMOD6jpxeeT03DfobWNKAbhvQ9pZ88X/O/LIN1cQnCC/q3ikQpFMx sVZLAk6PcRnR3RenMo6WhPRKEsbo3H/CzeGhuRDQSxYp+e2mxguZLZmvznptDCWh/XvO V8T4Qwsa7A91KabB3QMpWtdoLQUpIRWAQcFmRTPyFDuD1IP5XA7MOSLniFHFWTrduC0A JWyy74Y+pwOZSDG6xtMu/YNm2wFAyKXW6KvmO7N6SZFnsMckC6PAG/t20CYEaYxu5oP3 RtYw== X-Gm-Message-State: APjAAAX5Z0N2CcY7zWi+0zBomHkqtAAedWLB2ByA/bInktD4j1D5n0vM A37XOJlwy0K2roua2XBFMMjBeQVd2M/G8g== X-Received: by 2002:a17:90a:26ac:: with SMTP id m41mr2502408pje.59.1565334667285; Fri, 09 Aug 2019 00:11:07 -0700 (PDT) Received: from suzukaze.ipads-lab.se.sjtu.edu.cn ([89.31.126.54]) by smtp.gmail.com with ESMTPSA id 10sm506324pfv.63.2019.08.09.00.11.05 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 09 Aug 2019 00:11:06 -0700 (PDT) From: Chuhong Yuan Cc: linux-kernel@vger.kernel.org, Chuhong Yuan Subject: [PATCH v4 7/8] userns: Replace strncmp with str_has_prefix Date: Fri, 9 Aug 2019 15:11:03 +0800 Message-Id: <20190809071103.17442-1-hslester96@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org strncmp(str, const, len) is error-prone because len is easy to have typo. The example is the hard-coded len has counting error or sizeof(const) forgets - 1. So we prefer using newly introduced str_has_prefix() to substitute such strncmp to make code better. Signed-off-by: Chuhong Yuan --- Changes in v4: - Eliminate assignments in if conditions. kernel/user_namespace.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 8eadadc478f9..bd5702f9273a 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -1138,6 +1138,7 @@ ssize_t proc_setgroups_write(struct file *file, const char __user *buf, char kbuf[8], *pos; bool setgroups_allowed; ssize_t ret; + size_t len; /* Only allow a very narrow range of strings to be written */ ret = -EINVAL; @@ -1153,16 +1154,19 @@ ssize_t proc_setgroups_write(struct file *file, const char __user *buf, /* What is being requested? */ ret = -EINVAL; - if (strncmp(pos, "allow", 5) == 0) { - pos += 5; + + len = str_has_prefix(pos, "allow"); + if (len) { + pos += len; setgroups_allowed = true; + } else { + len = str_has_prefix(pos, "deny"); + if (len) { + pos += len; + setgroups_allowed = false; + } else + goto out; } - else if (strncmp(pos, "deny", 4) == 0) { - pos += 4; - setgroups_allowed = false; - } - else - goto out; /* Verify there is not trailing junk on the line */ pos = skip_spaces(pos); -- 2.20.1