From patchwork Mon Nov 13 16:15:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 134170 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C265E4331C; Mon, 13 Nov 2023 17:15:58 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C330240A89; Mon, 13 Nov 2023 17:15:39 +0100 (CET) Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by mails.dpdk.org (Postfix) with ESMTP id 468D1402D6 for ; Mon, 13 Nov 2023 17:15:36 +0100 (CET) Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-5bd306f86a8so3004376a12.0 for ; Mon, 13 Nov 2023 08:15:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1699892135; x=1700496935; 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=s/sj/v6CNc1SZY9jaOpzjGk52n0An3OSi6SCOvZCCvxG87IsuNbc8xc4AaMeEk3jc3 gLzibf0ViHDlIbQF3wTZzRnIZfseA0WrWYaflQKHMtA4Qspkj62jNyM0sQZXPUZlWxIt xmUfbWgMMXYa5ljQy8Udljy+bSowH9wa3UyQmDVSuQT3PwVWkSJ55JCIr77NswBrCz6v cDi6lFfjAVlaX00EWsByTBsdNH5PZwXGkVftW4yNxNW4+Q8gGqvc6Llrnry97ARHx/O/ NEvVDOz0Z9wFWRyxtLNgkw1i2rcY4MyRFn57uzOIRIbsYElED4gQWSm85LHLvDebwCQZ 5+3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699892135; x=1700496935; 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=lzBa4r4ZYUujsTx5ifCR/7xQ/kgLpxdVQXDW7KQY+3rS4qnXEeEqTyoa1XBH5O1pQs 5590RvTUjQd58q+VhAiHd5sJv1XSHV1L8E230QDCCnjZ+MnTpajt2NtxX6H6AgUff3Mo n7wVPAALm/V2e+ODHh0yXsVx4g8QQbpROtWrhvoQcrxRitAkqJlscsof7bGn3VlLaSBx Oa+nXKGQDaFQ4ji3+kjx0/cXOUd2bt0E2C8vVfQTHI1kad+4q3LNTCuo3otrohUK2KLb VWV8sRpKkKTpV+7uj+J+rL1YPpt93Uqjp6pgRLCinfD4U9dua4r2DNADB9diuAyKsJmj oWgw== X-Gm-Message-State: AOJu0YxQIFFgi0jkomEAtSzo9Zu4/alBqHSsnSWt82xEAeLpdQMUrYRQ n2tUYidclpMcsEdrGVUym8Tbsu+brnxMso327BA= X-Google-Smtp-Source: AGHT+IGuEwmWd75jahvN3/FWTkz3T+RtE31LQSGZ5VB+X3qE282Kmv3J0iFfcZ4x4VaYjlN4gm+kow== X-Received: by 2002:a05:6a21:3296:b0:181:9b65:b4b5 with SMTP id yt22-20020a056a21329600b001819b65b4b5mr5668735pzb.3.1699892135155; Mon, 13 Nov 2023 08:15:35 -0800 (PST) Received: from hermes.local (204-195-123-141.wavecable.com. [204.195.123.141]) by smtp.gmail.com with ESMTPSA id hq11-20020a056a00680b00b006c69bbf03e2sm3350043pfb.192.2023.11.13.08.15.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Nov 2023 08:15:34 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , =?utf-8?q?Morten_Br?= =?utf-8?q?=C3=B8rup?= , Reshma Pattan Subject: [PATCH v6 4/5] pcapng: avoid using alloca() Date: Mon, 13 Nov 2023 08:15:18 -0800 Message-Id: <20231113161519.242272-5-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231113161519.242272-1-stephen@networkplumber.org> References: <20230921042349.104150-1-stephen@networkplumber.org> <20231113161519.242272-1-stephen@networkplumber.org> MIME-Version: 1.0 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;