From patchwork Sun Nov 20 01:08:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Venekamp X-Patchwork-Id: 1706678 X-Patchwork-Delegate: hauke@hauke-m.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=jXWF22R9; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NFCDd3KWVz23nK for ; Sun, 20 Nov 2022 12:12:31 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To :From:Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=bMEsKMoaNMpEUzZ1CcO+7lqh4lTcFPgLKTkxMUNjKpU=; b=jXWF22R9ItV1VL AIsv9+6bZNaJhoCfNDHlsYBsqmLkjxmIoP1gYJ3IGwHs7RomwahuZgqQSXBpx702uHHDVaEaNF2mq lE3g0iOv6YkNE1v2QhnQGXar6ntxVUcYFEw8Ke6HLE/rUpdbmjbsGTM97mIXRn7iP4zzlqJ7XANLv pScjfHA9FNuBjw2MbhWYw//Vg6tj9oKz3H/IzQXkTS13EJM6nxekAr4fs7Nps+5vYjQoIzpfjOSUu YslmzCoKkEu+f3dE/Zqo8t3XpUNG7m+f5Q5fs5w+7/CdTeUKZzg570fao5+BcpKL8Yty2qn37ixs7 IyOQKl+Sk0uekUoT94Tw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1owYp6-000p7A-W4; Sun, 20 Nov 2022 01:08:41 +0000 Received: from virt1.bvwebdesign.nl ([149.210.228.112]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1owYp1-000p34-0g for openwrt-devel@lists.openwrt.org; Sun, 20 Nov 2022 01:08:36 +0000 Received: from localhost.localdomain (84-31-67-158.cable.dynamic.v4.ziggo.nl [84.31.67.158]) by virt1.bvwebdesign.nl (Postfix) with ESMTPSA id CECF7A5ED59 for ; Sun, 20 Nov 2022 02:08:28 +0100 (CET) From: Jan Venekamp To: openwrt-devel@lists.openwrt.org Subject: [PATCH v2 0/9] uci: fixes for uci_set and uci_add_list Date: Sun, 20 Nov 2022 02:08:19 +0100 Message-Id: <20221120010828.23765-1-jan@venekamp.net> X-Mailer: git-send-email 2.32.0 (Apple Git-132) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221119_170835_246428_83197ACB X-CRM114-Status: UNSURE ( 9.35 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: When developing a C application that updates options with uci_set I noticed that config files were not getting these updates. I found that this was due to a use-after-free bug in uci_set. Looking further at the code I also noticed some other issues. This patch series contains fixes to these issues as well as some other improvements. Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org When developing a C application that updates options with uci_set I noticed that config files were not getting these updates. I found that this was due to a use-after-free bug in uci_set. Looking further at the code I also noticed some other issues. This patch series contains fixes to these issues as well as some other improvements. Jan Venekamp (9): uci: fix use-after-free uci_set on update option uci: maintain option position in uci_set uci: optimize update option in uci_set uci: fix use-after-free uci_add_list uci: fix atomicity of uci_add_list uci: maintain option position in uci_add_list uci: fix memory leak uci_set on update section uci: optimize update section in uci_set uci: macro uci_alloc_element not in uci.h list.c | 168 ++++++++++++-------------- tests/shunit2/tests.d/090_cli_options | 8 +- uci.h | 10 -- uci_internal.h | 3 + 4 files changed, 87 insertions(+), 102 deletions(-) Interdiff against v1: diff --git a/list.c b/list.c index 89c70f0..1640213 100644 --- a/list.c +++ b/list.c @@ -734,10 +734,10 @@ int uci_set(struct uci_context *ctx, struct uci_ptr *ptr) strcpy(ptr->s->type, ptr->value); } else { struct uci_section *old = ptr->s; - ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section, &old->e.list); + ptr->s = uci_alloc_section(ptr->p, ptr->value, old->e.name, &old->e.list); uci_section_transfer_options(ptr->s, old); if (ptr->section == old->e.name) - ptr->section = ptr->o->e.name; + ptr->section = ptr->s->e.name; uci_free_section(old); ptr->s->package->n_section--; ptr->last = &ptr->s->e;