From patchwork Wed Aug 14 11:51:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pablo Neira Ayuso X-Patchwork-Id: 1972390 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=139.178.88.99; helo=sv.mirrors.kernel.org; envelope-from=netfilter-devel+bounces-3268-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [139.178.88.99]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkRS000cCz1yXl for ; Wed, 14 Aug 2024 21:51:43 +1000 (AEST) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 56D2D284A00 for ; Wed, 14 Aug 2024 11:51:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9CFC41AED58; Wed, 14 Aug 2024 11:51:31 +0000 (UTC) X-Original-To: netfilter-devel@vger.kernel.org Received: from mail.netfilter.org (mail.netfilter.org [217.70.188.207]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 371E219EEAF for ; Wed, 14 Aug 2024 11:51:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.188.207 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723636291; cv=none; b=R8NIAjodMLtaPSv6fKSw/VOhu7hI7Yxt3IivSLN3m+/3AVLVfjzRykt9ChFqIe60L7uvdgMvadBRlL0NwsCBZwC2WBspiFlEV6KXaE6K5H+16YgP8Bn0VpmSIn7DNY+xZI7BmjtxwNjiXZzCqpU6J+MNpLq6vDQBi3ZCcm6K+Ck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723636291; c=relaxed/simple; bh=ST7d80eua2AeyB50EWivcYO72kqvPaYj/cabc5JRSd8=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=Fms27L5OOiu9Xs+eZlcvyi11UH2mXAlet/yY19cYUcn/6D4yNpZSgqlQrTx++ndX7jvx9X3m9rFJyjqTYkWVs0eBAt2J/jHSM3z+oGJShAQl7SBTb2GGKncPRG6atitcdHYl0X9q16JK2FuxZOyJX0GFFJNGZVaQuR27ZDcQrhc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; arc=none smtp.client-ip=217.70.188.207 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netfilter.org From: Pablo Neira Ayuso To: netfilter-devel@vger.kernel.org Subject: [PATCH nft,v2 1/2] datatype: reject rate in quota statement Date: Wed, 14 Aug 2024 13:51:21 +0200 Message-Id: <20240814115122.279041-1-pablo@netfilter.org> X-Mailer: git-send-email 2.30.2 Precedence: bulk X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Bail out if rate are used: ruleset.nft:5:77-106: Error: Wrong rate format, expecting bytes or kbytes or mbytes add rule netdev firewall PROTECTED_IPS update @quota_temp_before { ip daddr quota over 45000 mbytes/second } add @quota_trigger { ip daddr } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ improve error reporting while at this. Fixes: 6615676d825e ("src: add per-bytes limit") Signed-off-by: Pablo Neira Ayuso --- v2: - change patch subject - use strndup() to fetch units in rate_parse() so limit rate does not break. src/datatype.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/datatype.c b/src/datatype.c index d398a9c8c618..297c5d0409d5 100644 --- a/src/datatype.c +++ b/src/datatype.c @@ -1485,14 +1485,14 @@ static struct error_record *time_unit_parse(const struct location *loc, struct error_record *data_unit_parse(const struct location *loc, const char *str, uint64_t *rate) { - if (strncmp(str, "bytes", strlen("bytes")) == 0) + if (strcmp(str, "bytes") == 0) *rate = 1ULL; - else if (strncmp(str, "kbytes", strlen("kbytes")) == 0) + else if (strcmp(str, "kbytes") == 0) *rate = 1024; - else if (strncmp(str, "mbytes", strlen("mbytes")) == 0) + else if (strcmp(str, "mbytes") == 0) *rate = 1024 * 1024; else - return error(loc, "Wrong rate format"); + return error(loc, "Wrong unit format, expecting bytes, kbytes or mbytes"); return NULL; } @@ -1500,14 +1500,20 @@ struct error_record *data_unit_parse(const struct location *loc, struct error_record *rate_parse(const struct location *loc, const char *str, uint64_t *rate, uint64_t *unit) { + const char *slash, *rate_str; struct error_record *erec; - const char *slash; slash = strchr(str, '/'); if (!slash) - return error(loc, "wrong rate format"); + return error(loc, "wrong rate format, expecting {bytes,kbytes,mbytes}/{second,minute,hour,day,week}"); + + rate_str = strndup(str, slash - str); + if (!rate_str) + memory_allocation_error(); + + erec = data_unit_parse(loc, rate_str, rate); + free_const(rate_str); - erec = data_unit_parse(loc, str, rate); if (erec != NULL) return erec; From patchwork Wed Aug 14 11:51:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pablo Neira Ayuso X-Patchwork-Id: 1972391 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=147.75.80.249; helo=am.mirrors.kernel.org; envelope-from=netfilter-devel+bounces-3269-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from am.mirrors.kernel.org (am.mirrors.kernel.org [147.75.80.249]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WkRS31Cs2z1yXl for ; Wed, 14 Aug 2024 21:51:47 +1000 (AEST) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id D062C1F232DD for ; Wed, 14 Aug 2024 11:51:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D853A1B0102; Wed, 14 Aug 2024 11:51:31 +0000 (UTC) X-Original-To: netfilter-devel@vger.kernel.org Received: from mail.netfilter.org (mail.netfilter.org [217.70.188.207]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E78621AC443 for ; Wed, 14 Aug 2024 11:51:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.188.207 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723636291; cv=none; b=GFKMLB1XIYacOQNk9M7D6rl7lwh+GN4+nQfdW0MWxvRTMcO6zKEFkldS/jm6mFNPDZ/QYrcl6sCePwNjY+X8yB1hRS+YstVQN0r+BG7rigSzB7YgpfB6xlxr7DeP2uc/w7tHFUV8CKUtpLaAota4dDzWgyKTl7veh5Mx197r/7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723636291; c=relaxed/simple; bh=e8py/eIhYY8Tu2EdmcNEa9U+uCqbPQpX0smqrMySEeM=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FNhWrPqPzl/YV0qWE1T2Xu1jBQZvJRbqC82kBsRTTs0xrYJ+dWhEpfwf47WuzX8QFxihRcLN6AS3aoIYPEbknR3uZQAapl5EloVAYBcFY6SS+prZ4aUyLB0sE+w+7hneOXcfuHWJnbO2CdzHuJ+BxwHQQzxQJQApKpeqpml9SwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; arc=none smtp.client-ip=217.70.188.207 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netfilter.org From: Pablo Neira Ayuso To: netfilter-devel@vger.kernel.org Subject: [PATCH nft,v2 2/2] datatype: improve error reporting when time unit is not correct Date: Wed, 14 Aug 2024 13:51:22 +0200 Message-Id: <20240814115122.279041-2-pablo@netfilter.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240814115122.279041-1-pablo@netfilter.org> References: <20240814115122.279041-1-pablo@netfilter.org> Precedence: bulk X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Display: Wrong unit format, expecting bytes or kbytes or mbytes instead of: Wrong rate format Fixes: 6615676d825e ("src: add per-bytes limit") Signed-off-by: Pablo Neira Ayuso --- src/datatype.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/datatype.c b/src/datatype.c index 297c5d0409d5..33fe471048ef 100644 --- a/src/datatype.c +++ b/src/datatype.c @@ -1477,7 +1477,7 @@ static struct error_record *time_unit_parse(const struct location *loc, else if (strcmp(str, "week") == 0) *unit = 1ULL * 60 * 60 * 24 * 7; else - return error(loc, "Wrong rate format"); + return error(loc, "Wrong time format, expecting second or minute or hour or day or week"); return NULL; }