From patchwork Tue Sep 12 15:05:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 138700 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp940930vqx; Wed, 13 Sep 2023 01:47:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGSiyj8aKUtAEoqODEDBw3hyJBkEbyrrCNRUJKfDfRay8OcakP4dCzsht9T+t6fMaML+RRm X-Received: by 2002:a05:6870:c58c:b0:1b0:454b:1c3d with SMTP id ba12-20020a056870c58c00b001b0454b1c3dmr2155475oab.36.1694594840594; Wed, 13 Sep 2023 01:47:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694594840; cv=none; d=google.com; s=arc-20160816; b=tjhhVLfGsBxNWqijvmU64Krq9T5E2/ix5Sf0Tqaa6Em9CumkRurHypbUjtdR3irLfc 9QkHyUm1bHiNGvSsbxkkrWC8PFbqYoggtORhzVUi+lO/DKpzUhQPstqrbzVRrdW3jTVK aperGPYAW7zlOk4qDhLUVqpU5xJKmPnf5mEIwgr25uZ/KiwJJVX3w7/sjoLjsqcqdxVj MK+6lGoFy0VQHTAktgfaTse9gWtjvxBcG99dMq79bCLy0qsetXMFIqb4gKTQl7km5s55 Q0sKdMtiBZyBg/YzzbsD1nc1VbbpB6TIEo9AE7FBXV8ebBk/a2S6iLlxxE388rGfhFg4 fuuQ== 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 Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id d64-20020a633643000000b0057764b2f3dfsi5977181pga.198.2023.09.13.01.47.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 01:47:20 -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 6001D808E645; Tue, 12 Sep 2023 08:07:34 -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 S236272AbjILPHb (ORCPT + 37 others); Tue, 12 Sep 2023 11:07:31 -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 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:34 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776855018458062678 X-GMAIL-MSGID: 1776911479331124450 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;