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 8AE7A432D9; Wed, 8 Nov 2023 19:36:42 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8D0D942DD1; Wed, 8 Nov 2023 19:36:16 +0100 (CET) Received: from mail-oa1-f53.google.com (mail-oa1-f53.google.com [209.85.160.53]) by mails.dpdk.org (Postfix) with ESMTP id 8A04941156 for ; Wed, 8 Nov 2023 19:36:13 +0100 (CET) Received: by mail-oa1-f53.google.com with SMTP id 586e51a60fabf-1ea48ef2cbfso4338036fac.2 for ; Wed, 08 Nov 2023 10:36:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1699468572; x=1700073372; 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=Vsmc5T89G0SYjUfKbvLN3pvhlouN8b2pD5fYyKZ0XzU=; b=aYxZcdMWLmid76jgjWpnVIgJBjfL/JJ/fB3bMBNdjfjiyTHRlx174okOS2Hreb+dRN X6d7zTtMmFqiui9KQS6rfKTq8ku7gvW9G5P2vwlcRgQp4nrSsBwHXhObHcWxpnwQMu6w i0KE5oW2t+N5HnFSF8z4skxC+t8sr7fHJomm7UI3TlK8TvhZyTUFThjnsCnjEIiNMIqL cfBN+WQx4du0ZPzFXm29f3P9HWGcMq5EmHiryh6S5Th48UFtb/NbyDU9jUDUD7Negsy7 qMkQcPO1lMR9Ib0b524fNYib/y7/5Nblc6ZRyalaH7BATo4QWlmbCf6BEa/f4UL9OZKR 3P4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699468572; x=1700073372; 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=Vsmc5T89G0SYjUfKbvLN3pvhlouN8b2pD5fYyKZ0XzU=; b=RpKxXSbenlEG6gF5Cwn2ubTo7Naw4DfWyHyEXhoHO+eTPeL8I88IVPbq9RgH7IHNpw P0tmNzUU53pD7OKAKk3T4akrYCkZi65OTm0in1BUfyyTGwbM2qf53KZs2S/RHJCEjoEy G79LU4VJeWFRhJbGSgMfD/NWWJDV2yQdK0WaraKD2w9a/gAMQb8MMfCq+CFCf+3Ri0mL JdN0H3dx7xugiRXq47YKMiRdaiomzmuzjnwc3bzWn6sfv4270q4/PYnUsw+7642ggYKe uXSVXpl0cq565yd+kKGR4S1MbH6TrUpmrcy4AAtYtrY75GZIw+71NzPXQaF/zzfhz46h jc7Q== X-Gm-Message-State: AOJu0Yxeskl+fOCjUZNvgop7yNUkFVhp+ze76TNeJ/kMngtpgimcfFm3 2srnJ3eU3yz/85fzpIRVzpt4dSiODo4MwSsNpJ4= X-Google-Smtp-Source: AGHT+IGmd9aF3GyMSMsPN8/QZzYf4kJ0Y1KZPnQeK0VWpTDHO1GPixIxIS+KmGy/exapLoaGII3pFw== X-Received: by 2002:a05:6871:60c:b0:1ea:60c2:9fcd with SMTP id w12-20020a056871060c00b001ea60c29fcdmr2960175oan.47.1699468572582; Wed, 08 Nov 2023 10:36:12 -0800 (PST) Received: from hermes.local (204-195-123-141.wavecable.com. [204.195.123.141]) by smtp.gmail.com with ESMTPSA id p25-20020a635b19000000b005b944b20f34sm3402522pgb.85.2023.11.08.10.36.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 10:36:11 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v3 4/5] pcapng: avoid using alloca() Date: Wed, 8 Nov 2023 10:35:56 -0800 Message-Id: <20231108183557.381955-5-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231108183557.381955-1-stephen@networkplumber.org> References: <20230921042349.104150-1-stephen@networkplumber.org> <20231108183557.381955-1-stephen@networkplumber.org> MIME-Version: 1.0 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 (4K) which will be more than sufficient for the info related blocks in the file. Add bounds checks as well. Signed-off-by: Stephen Hemminger --- lib/pcapng/rte_pcapng.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c index 13fd2b97fb80..67f74d31aa32 100644 --- a/lib/pcapng/rte_pcapng.c +++ b/lib/pcapng/rte_pcapng.c @@ -140,9 +140,8 @@ pcapng_section_block(rte_pcapng_t *self, { struct pcapng_section_header *hdr; struct pcapng_option *opt; - void *buf; + uint8_t buf[BUFSIZ]; uint32_t len; - ssize_t cc; len = sizeof(*hdr); if (hw) @@ -158,8 +157,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 +191,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 +208,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[BUFSIZ]; char ifname_buf[IF_NAMESIZE]; char ifhw[256]; uint64_t speed = 0; @@ -267,8 +262,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 +290,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 +326,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[BUFSIZ]; RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL); @@ -353,8 +346,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