From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6C0AD432EA; Thu, 9 Nov 2023 20:46:32 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id F35C4410E6; Thu, 9 Nov 2023 20:46:14 +0100 (CET) Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) by mails.dpdk.org (Postfix) with ESMTP id 6772140FDE for ; Thu, 9 Nov 2023 20:46:12 +0100 (CET) Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3b2ec5ee2e4so721381b6e.3 for ; Thu, 09 Nov 2023 11:46:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1699559171; x=1700163971; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=N2r/H5v/jMSX7aFlXgmw6wyhS0CLdclJuU10KJ1gaU0=; b=w2jkR+LQGS3Ok2XDK0nf51txb+rTLqIF6L1F5HcftH9lObpMYIZzlG5GgmXnMDVAwn hn/oCCYxtZYkR+FXp5BYOu6yiizwjmaIgt46bRI0U1VCS3ikUuJnhYuIhLDQDj++kDkW hGkdnBau/uNBBKp5XvSGfMN6y7FxUy+Cy7k9hJX6PPcIjm5BMd1TgBHDr5Hqes1QDc+Y yfGFXmG6It/awU6UZVeFBPAkmE+DAvs7kyz2FF0KvHbw6rdxTKXTPcuKQCjSfCpYHYhK nViMZQwfPmXGCXdHqQIsUOMuCGD0QbWzKwFLAEDtQXI5SiGWqSHBaku3kZjGhVQpdCKz xMrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699559171; x=1700163971; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N2r/H5v/jMSX7aFlXgmw6wyhS0CLdclJuU10KJ1gaU0=; b=cusWWGhxomcQBVMjwdBewwZbQ5KVuhWgEdX03Tah8roRxOlTzko+E3p+h3LXOSG4mx UohVoGOOn8W15WrcOojXaLmY/XVkOhJg9N6uQGwnznTZYuBfCULpF0s0cDeLjWC1HHS5 3t3yBN4mJuWOkzDlwfofwJZFuXz2haKHzHAydahF5EhPzebhYDkPcw3PR99EEXzkBS1f d/R/v4kHCEpnPFeUW6axtSB0CtKzDR8nUWbZY/XYlChTVQRLPtbUjSVJAiL94gUXRs/d eNJhuBKqtZPa7CJXnvHEw4hgvLm8dlSeiH435uKVC6P4T7xr6SlZuj1PaDjtUg1L1iaX +mFw== X-Gm-Message-State: AOJu0YyAUXrMaKd7VaK/rmGN/TtS+BmPaaI6mdwwZKqDWBU+G4UffNtx U8dkutbfKW8+74jlCrKC3boIx9nczSj6Tzgq1ow= X-Google-Smtp-Source: AGHT+IFauCYlcgxMWspov9IFoxi1G5lIjcCsttMqllkftbv4cewyLY/hwtaO9AT8YDyi/GG9Np1keA== X-Received: by 2002:a05:6808:292:b0:3a6:febf:e1 with SMTP id z18-20020a056808029200b003a6febf00e1mr2671180oic.55.1699559171489; Thu, 09 Nov 2023 11:46:11 -0800 (PST) Received: from hermes.local (204-195-123-141.wavecable.com. [204.195.123.141]) by smtp.gmail.com with ESMTPSA id j191-20020a6380c8000000b0059b782e8541sm5176101pgd.28.2023.11.09.11.46.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 11:46:10 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , =?UTF-8?q?Morten=20Br=C3=B8rup?= , Reshma Pattan Subject: [PATCH v5 4/5] pcapng: avoid using alloca() Date: Thu, 9 Nov 2023 11:45:56 -0800 Message-Id: <20231109194557.111350-5-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231109194557.111350-1-stephen@networkplumber.org> References: <20230921042349.104150-1-stephen@networkplumber.org> <20231109194557.111350-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The function alloca() like VLA's has problems if the caller passes a large value. Instead use a fixed size buffer (2K) which will be more than sufficient for the info related blocks in the file. Add bounds checks as well. Signed-off-by: Stephen Hemminger Acked-by: Morten Brørup --- lib/pcapng/rte_pcapng.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c index 13fd2b97fb80..f74ec939a9f8 100644 --- a/lib/pcapng/rte_pcapng.c +++ b/lib/pcapng/rte_pcapng.c @@ -33,6 +33,9 @@ /* conversion from DPDK speed to PCAPNG */ #define PCAPNG_MBPS_SPEED 1000000ull +/* upper bound for section, stats and interface blocks */ +#define PCAPNG_BLKSIZ 2048 + /* Format of the capture file handle */ struct rte_pcapng { int outfd; /* output file */ @@ -140,9 +143,8 @@ pcapng_section_block(rte_pcapng_t *self, { struct pcapng_section_header *hdr; struct pcapng_option *opt; - void *buf; + uint8_t buf[PCAPNG_BLKSIZ]; uint32_t len; - ssize_t cc; len = sizeof(*hdr); if (hw) @@ -158,8 +160,7 @@ pcapng_section_block(rte_pcapng_t *self, len += pcapng_optlen(0); len += sizeof(uint32_t); - buf = calloc(1, len); - if (!buf) + if (len > sizeof(buf)) return -1; hdr = (struct pcapng_section_header *)buf; @@ -193,10 +194,7 @@ pcapng_section_block(rte_pcapng_t *self, /* clone block_length after option */ memcpy(opt, &hdr->block_length, sizeof(uint32_t)); - cc = write(self->outfd, buf, len); - free(buf); - - return cc; + return write(self->outfd, buf, len); } /* Write an interface block for a DPDK port */ @@ -213,7 +211,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, struct pcapng_option *opt; const uint8_t tsresol = 9; /* nanosecond resolution */ uint32_t len; - void *buf; + uint8_t buf[PCAPNG_BLKSIZ]; char ifname_buf[IF_NAMESIZE]; char ifhw[256]; uint64_t speed = 0; @@ -267,8 +265,7 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, len += pcapng_optlen(0); len += sizeof(uint32_t); - buf = alloca(len); - if (!buf) + if (len > sizeof(buf)) return -1; hdr = (struct pcapng_interface_block *)buf; @@ -296,17 +293,16 @@ rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, opt = pcapng_add_option(opt, PCAPNG_IFB_HARDWARE, ifhw, strlen(ifhw)); if (filter) { - /* Encoding is that the first octet indicates string vs BPF */ size_t len; - char *buf; len = strlen(filter) + 1; - buf = alloca(len); - *buf = '\0'; - memcpy(buf + 1, filter, len); + opt->code = PCAPNG_IFB_FILTER; + opt->length = len; + /* Encoding is that the first octet indicates string vs BPF */ + opt->data[0] = 0; + memcpy(opt->data + 1, filter, strlen(filter)); - opt = pcapng_add_option(opt, PCAPNG_IFB_FILTER, - buf, len); + opt = (struct pcapng_option *)((uint8_t *)opt + pcapng_optlen(len)); } opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0); @@ -333,7 +329,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, uint64_t start_time = self->offset_ns; uint64_t sample_time; uint32_t optlen, len; - uint8_t *buf; + uint8_t buf[PCAPNG_BLKSIZ]; RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL); @@ -353,8 +349,7 @@ rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id, optlen += pcapng_optlen(0); len = sizeof(*hdr) + optlen + sizeof(uint32_t); - buf = alloca(len); - if (buf == NULL) + if (len > sizeof(buf)) return -1; hdr = (struct pcapng_statistics *)buf; -- 2.39.2