Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3223443rdb; Wed, 13 Sep 2023 06:05:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEjgd/TsPXgS7o/5Ev+WG7YpZhRYkR9EFagCQKA9CDVASVoUAotq7uSuwthfsoqp/tisrj/ X-Received: by 2002:aa7:88c3:0:b0:68f:e245:3aa5 with SMTP id k3-20020aa788c3000000b0068fe2453aa5mr2857717pff.33.1694610353033; Wed, 13 Sep 2023 06:05:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694610353; cv=none; d=google.com; s=arc-20160816; b=beAb861kWN2WKyJ8p3TK+gw8tYHFWBb9kwU7Kqa81OAZYRgOITFSifJDdkNl74HiB2 twHPohIoOByd847ruoIr9QLuan2irvb/bYTxegzfcTwePxMj9WyItvuW6ftqWzN0pjRp Pt1lugs7xpk78KzN4mPhMe/0nSvKp3nAZqiGmBhuwGexB8ASjZOoAhfIKBDG1IMx0sTB 4iHnFHN2k3dS0xFknNmxDwKfwRqoZedEwmj+s/bFgzecCN6vB45kzhec3aaGIjLsosLW rKf7PWSDywoZBTK+T83MQr31l4QHBNVDh219wvdyYzVsiUCkGHUVzzSVzJWfpP+c/IXj INUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8mpuZnPu7gN0UOAsRR26oEI8C+csu2SNjSkUkq/OD2U=; fh=bHBp+PJACSvucKAvnO3bf9DILRtfHT5RT33jsVWkQ4A=; b=sW7SI9ZS2kTTou6t8Hf6eC56FsSuGyfRaSwvF8EhLYN6uXlxxRGYcvRYlubF4Vp8Sv E3JTTqF9/qJD8fmY5DolnG1FRRTYH2Paa81LEfSBRMSZb1WsP29TuZRbO4/HHry+DqwN xklA99n15X9eCZ7zOccF8EvoAE1fMURkfVnVf2DoSszpNX8niH1ZinXHdQGDeygUQfcB CCTQjfT4EcTIdwk5INoNJ5trsAmPgQa8LsPvdOyNtwC+e4TfvezptLh1QOZOcZgFDi5f ljeoMq53hrlVZaAvHZOQsEi4+yR9sOYDLUPglUhLN6pU+O6iAt2PNJREMCT/1I91NuDb odCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Vv4fQh64; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id cw25-20020a056a00451900b0068fdfe29f0asi3800640pfb.88.2023.09.13.06.05.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 06:05:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Vv4fQh64; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 82893808E64E; Tue, 12 Sep 2023 08:07:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236281AbjILPHa (ORCPT + 99 others); Tue, 12 Sep 2023 11:07:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236207AbjILPHV (ORCPT ); Tue, 12 Sep 2023 11:07:21 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DC4A10DC for ; Tue, 12 Sep 2023 08:07:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694531234; x=1726067234; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=W3PADM1uFv8tnSLMvvhvYaZtPehE/6YKdLW1n09TF9U=; b=Vv4fQh649KrhtEOhWDDiluxQ0RQ9PNd5alaPppENkXKbD5RwjbUW6LX5 tId8+i2o4KaYVtfD3oYpWalvJzIUiTzlU5iX18fOeCj39c2RtXxyii1yc TB+idrCJ5iDf24Tp1ha7Gjchx+xzVP+r1q7i7TMIYPJNuF7MwlQIXnjov BwrTXE4IS37O4sia6xq7tVL9Mkm4KW445hem7DAQxwNTxXLJ/G9TacbHQ u4BzCK7JpVhxDFANvni7wK4ekhO6v7KNqWVF094Z4cMjTuum3/xpvKQ+f c7atHFUsAcgvONPiaRu4XkfKqv4DVZa++F7SEYXMdDd4rrDUF7p9gr989 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="409356435" X-IronPort-AV: E=Sophos;i="6.02,139,1688454000"; d="scan'208";a="409356435" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2023 08:05:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="858836453" X-IronPort-AV: E=Sophos;i="6.02,139,1688454000"; d="scan'208";a="858836453" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga002.fm.intel.com with ESMTP; 12 Sep 2023 08:05:55 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 787C8BC0; Tue, 12 Sep 2023 18:05:54 +0300 (EEST) From: Andy Shevchenko To: Andy Shevchenko , linux-kernel@vger.kernel.org Cc: Luis Chamberlain Subject: [PATCH v1 3/6] params: Do not go over the limit when getting the string length Date: Tue, 12 Sep 2023 18:05:48 +0300 Message-Id: <20230912150551.401537-3-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230912150551.401537-1-andriy.shevchenko@linux.intel.com> References: <20230912150551.401537-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Sep 2023 08:07:35 -0700 (PDT) We can use strnlen() even on early stages and it prevents from going over the string boundaries in case it's already too long. Signed-off-by: Andy Shevchenko --- kernel/params.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/kernel/params.c b/kernel/params.c index fb594132ffc7..930a5dc2f004 100644 --- a/kernel/params.c +++ b/kernel/params.c @@ -259,7 +259,10 @@ EXPORT_SYMBOL_GPL(param_set_uint_minmax); int param_set_charp(const char *val, const struct kernel_param *kp) { - if (strlen(val) > 1024) { + size_t len, maxlen = 1024; + + len = strnlen(val, maxlen + 1); + if (len == maxlen + 1) { pr_err("%s: string parameter too long\n", kp->name); return -ENOSPC; } @@ -269,7 +272,7 @@ int param_set_charp(const char *val, const struct kernel_param *kp) /* This is a hack. We can't kmalloc in early boot, and we * don't need to; this mangled commandline is preserved. */ if (slab_is_available()) { - *(char **)kp->arg = kmalloc_parameter(strlen(val)+1); + *(char **)kp->arg = kmalloc_parameter(len + 1); if (!*(char **)kp->arg) return -ENOMEM; strcpy(*(char **)kp->arg, val); @@ -507,7 +510,7 @@ int param_set_copystring(const char *val, const struct kernel_param *kp) { const struct kparam_string *kps = kp->str; - if (strlen(val)+1 > kps->maxlen) { + if (strnlen(val, kps->maxlen) == kps->maxlen) { pr_err("%s: string doesn't fit in %u chars.\n", kp->name, kps->maxlen-1); return -ENOSPC; -- 2.40.0.1.gaa8946217a0b