From patchwork Fri Nov 17 16:35:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 134467 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 668D243355; Fri, 17 Nov 2023 17:38:12 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9B70F42D78; Fri, 17 Nov 2023 17:37:48 +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 D4429427E8 for ; Fri, 17 Nov 2023 17:37:45 +0100 (CET) Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3b2e330033fso1354882b6e.3 for ; Fri, 17 Nov 2023 08:37:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1700239065; x=1700843865; 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=CgabqZvyBZM7J+fRAWDp8OWCDPA2H+xWnMviLGA3Mzw=; b=fRhHHG7afbdKjJbAYfoO25wCjCf0ysOPWIDynkiKc7ximl/3efjjqBdWdQwkZyvgmL /SgcEY+4oH1YRfdQsmQD0xvPsopcjeGhklMln8X11/EFZiPi+H9VTR2lHbFvBpyLo5rm HstDCcs2+oTMkJ+TJIll7EyZ8SMt6S+kM0N94CaxxDz0mVjL05L6p/9i8/mgtS23Jcct ZXUAmJxnMVy7FbqNrUAKRv7rYc1F7xocpjaPxLa3da0M/nk46AKBz/GripLBgmftBzgc qUkIcy/Nb7bMMLVmloQ5XQo0Q0EuvRO5v44/gf3QBElM2ndY0Uq1YkoHljaW/yZpfOgV tUhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700239065; x=1700843865; 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=CgabqZvyBZM7J+fRAWDp8OWCDPA2H+xWnMviLGA3Mzw=; b=oCo7/9D8jw/K8K1IFttB10u9X2kXEcNCgDNKCXednVS816YX5Estygxp5DVTw7n5TL iH0sybXCnsI41qISWpH1dTZ1IY6LYgMpV6bSs2vopQYxhoR53ZToSHXO++vsOpwtTEs1 1fI+Ds+VLRSpxpcYePKfBlONHwf9HJ7bL0XRJs6qq23bzAHfA0rGhSdDfzW02mowoMYz EV+OnTMn3UwEpYme6EqsXf2CeayOU9ot0bsn5SXA5RcyC8Z23yVS7zpXYyWl5X8bFGZH 5LLcqGS5NzKhwovcoMTR1FtSg7nub3h5nfg+/+QLKyfcn2VuT+8tvFvJ9Yubf9LJvNgu aHxg== X-Gm-Message-State: AOJu0YzHZcYUumJR/nt6QmKg6XY2MJuROdBfV+VxCILiPt/cPBi4NxDs gJRxN6CQfHxgKbmgLzVTSEBeIgBr5bJ5d9QUciBO0g== X-Google-Smtp-Source: AGHT+IGsEt+ABZqJKO8D7wmMlkqpDe0wQhA3sELZ1D4MXedWxIoJTBI+iGxeZ3rPzvz2oIXvjRmGjw== X-Received: by 2002:a05:6808:4395:b0:3a7:1d15:28fe with SMTP id dz21-20020a056808439500b003a71d1528femr18890021oib.56.1700239065045; Fri, 17 Nov 2023 08:37:45 -0800 (PST) Received: from hermes.local (204-195-123-141.wavecable.com. [204.195.123.141]) by smtp.gmail.com with ESMTPSA id f22-20020a631016000000b005bd3f34b10dsm1595541pgl.24.2023.11.17.08.37.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Nov 2023 08:37:44 -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 v7 4/5] pcapng: avoid using alloca() Date: Fri, 17 Nov 2023 08:35:58 -0800 Message-ID: <20231117163729.243188-5-stephen@networkplumber.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231117163729.243188-1-stephen@networkplumber.org> References: <20230921042349.104150-1-stephen@networkplumber.org> <20231117163729.243188-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;