Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp309029ybi; Thu, 1 Aug 2019 19:33:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqz+IjFme2qrh1TdioRAb7A1MbfEZoWywgruKTiM1OgfI83pt0IMZzgVYkcsjZVtxZONFtW7 X-Received: by 2002:a63:d30f:: with SMTP id b15mr120863213pgg.341.1564713205502; Thu, 01 Aug 2019 19:33:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564713205; cv=none; d=google.com; s=arc-20160816; b=GJG7OO0uhe+nGenWU8z9dlIE1OI9lM63xo3Nk3ceRdVFmZN2RW9ysbEtQT3vq5OywI sfaiSQNN0gEWgeIqn2HcHVPObziwiACCMENjhY79HU2tzo98CTvYLiVkUCyD9OgdxMzK OHBtukjoNYTrzjRjKhuk1q75KR5Bm9ciQ/IzcGEkTe6ZR20d1Dt/818A3tctqIbMVGmk YSCY2h2d94zC0la3r/nLxSkysCX37CoXABgtgIGDWI+TD5LhT4PTNIp1dZdzc/0X7FJ/ KMb2ZzEhJFM41RJGIUC5gcvdLZMdyTpxjjXvGz2yEzn4o/s1bDl1fU4Z04sPnNJyfelk dWxg== 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=iq2Oofr0Gl/QXG71RCvyorbUHlM5X2J3tGq+6DkFErQ=; b=GOtaoxDKBUjIyu/zAkBSIJ9fOs42cxBwWJXEo59b9812hrU3SA+MzpKJ2jly5CfJj6 LBkdtVSgMBiJ9OojH4xE4tDwdVyzgEbe5+eKG301DHBaKiIUQWM0skmZS0lPYU97kisU XrFmgIAke/l16sd4kXB4zUbZoS0p29WuslRKC8gO5MxYn/5pyWfoZv1cLYcZ1RxDE4/M YfekkOJwsAhG2shHYEZ/0ck8c6fFc6ZBVFP6qrpTAdTschf0asf0c0nqf2S/tqDH8Djz wMAGEbBq0nUqC6R84fGG88kr+Og0X0AHeDxg2eOLFObBBCNNKekapsr66KdTMGOUGW/H xk0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=e2yaFF4R; 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 1si39218362pgu.504.2019.08.01.19.33.10; Thu, 01 Aug 2019 19:33:25 -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=e2yaFF4R; 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 S2390634AbfHBBrp (ORCPT + 99 others); Thu, 1 Aug 2019 21:47:45 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:45545 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727630AbfHBBrp (ORCPT ); Thu, 1 Aug 2019 21:47:45 -0400 Received: by mail-pg1-f193.google.com with SMTP id o13so35178134pgp.12 for ; Thu, 01 Aug 2019 18:47:45 -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=iq2Oofr0Gl/QXG71RCvyorbUHlM5X2J3tGq+6DkFErQ=; b=e2yaFF4RWbkfUO1QTe/GDUHUpJSDfgDyYAIWk48MdUvp5QrvvaUQ6P/FVAQDGa+299 JOljzkOXkDdiCitVXPbnl+Ck0/SO4chaRleTHKKlGV8WVVCUn9aBOfk/i102nO37OrgF 278Xh/W+vjFHrksBgLaRfT302DmiCnZrW89qLs3Of2eMCG6H58gm7wo8xqT3XS309JH+ LBG7rfz8JPA9gkiRjfJC5Gy/UR3j4Gxc7h8m0UQbfwpfa477PBa4K1oWxStVlAZg5jWm bl5+a0DUnWxqzX+MrzFNxTCPCKvMS9snPerBRUyXrNx7yUG9JP3dR+GLNqlCoGY0gqd5 0CBg== 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=iq2Oofr0Gl/QXG71RCvyorbUHlM5X2J3tGq+6DkFErQ=; b=DCkm6/OZ7Yo7uKElXkxKUzyLJL/5rL4VTuJBiAmsRicSIsh2s46TdG1a+HbSPcpR2r zaIjeu23W+6QSFjGmLiBg3QZDMNocSjHriwqWPs5BWVyrrOH/pW3fItGsgYRcoMYe+eg y3/VAryEAkH3X7Sb6r0PymKYR0jcG6xflayTcNNBpsW8JzoVbu4wiH9Ko/39G8OpYsfI eQXjja+o6yrqJo5nbv+ySTY143Fq0AKbsF6PseZ9ZAJBDf+KxOfkoGuFOStVb75A95SL dcyGYny7H0QpxPs51fHlHQjTgnqE0a/HaXeA7HLdgmaQdsvf8Aa7t/LZyPpmeAziAq0R 3Scw== X-Gm-Message-State: APjAAAUUPsPM22WvmBYlOsvuJXhgxa2JM1tkJd37UoHF49O6eIQfM1wp fo7YdaEs2htCI33O+hQQCYksiZ8BIY2jfg== X-Received: by 2002:a62:6d84:: with SMTP id i126mr55568288pfc.129.1564710464729; Thu, 01 Aug 2019 18:47:44 -0700 (PDT) Received: from suzukaze.ipads-lab.se.sjtu.edu.cn ([89.31.126.54]) by smtp.gmail.com with ESMTPSA id w2sm63002010pgc.32.2019.08.01.18.47.42 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 01 Aug 2019 18:47:44 -0700 (PDT) From: Chuhong Yuan Cc: Greg Kroah-Hartman , linux-kernel@vger.kernel.org, Chuhong Yuan Subject: [PATCH v2 09/10] userns: Replace strncmp with str_has_prefix Date: Fri, 2 Aug 2019 09:47:39 +0800 Message-Id: <20190802014739.9114-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. Signed-off-by: Chuhong Yuan --- Changes in v2: - Revise the description. - Utilize str_has_prefix's return value to eliminate some hard codes. kernel/user_namespace.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 8eadadc478f9..e231e902df8a 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,12 +1154,11 @@ 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; + if ((len = str_has_prefix(pos, "allow"))) { + pos += len; setgroups_allowed = true; - } - else if (strncmp(pos, "deny", 4) == 0) { - pos += 4; + } else if ((len = str_has_prefix(pos, "deny"))) { + pos += len; setgroups_allowed = false; } else -- 2.20.1