From patchwork Mon Sep 14 14:04:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1363612 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=IiE52j6+; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=hL5dqFyz; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Bqp8Y1L12z9sTp for ; Tue, 15 Sep 2020 00:07:24 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=et8Rs8/flshg+M9p3rbz7rGrEjPxawizp4vD5P1FPiI=; b=IiE52j6+1RlJrdUvVf9SPEqcRF Moii2v+GuTY2evVU+m23yf8tQu+dfkpicZZKYCQpRmtyRVPNWQ8iUoVP0MtAA01v/sGb8KN3RukFs G5H8HvRuKbaalrZCshcxeQeykXrRGDTUZbRxCVgOCTpWcf9X6npHRuTj5Ct/+VI8ZfTSNHRVfHPGX 30730QsvKkSbvtqQrBjeU4O2ne1/mIPwEA8lJg1jrS7y3WgODGGfEODyRMhGgHndvhV8AMYqgbgkH v5pFFaO+ClRN+GOmhuB/8MJAWCQ9EJ+tZDaGJHsNNUmu/z6K7RaPkheWEKAs+Lb2sQkaRlFbbCtsm 60eXou1w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kHp6N-0004KO-53; Mon, 14 Sep 2020 14:05:03 +0000 Received: from mail-lf1-x142.google.com ([2a00:1450:4864:20::142]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kHp6K-0004JF-3Q for openwrt-devel@lists.openwrt.org; Mon, 14 Sep 2020 14:05:00 +0000 Received: by mail-lf1-x142.google.com with SMTP id u8so13584028lff.1 for ; Mon, 14 Sep 2020 07:04:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4XL67LggwPcaSb5QCcWsfj8MUU09ylvqAEtbdq8REkw=; b=hL5dqFyzvz0XM3gfnevjeLdt3/EWauIg2pxJaakd76g6RGbMQnU16GTP3a0okYOl+d XcAY332/mROJbLaz9pxp/UxxX1CnVwUAzh1w80J6TiZKOhLjgCsTUT3M/3F634TT7xKO w7vrxVkwu4f0nkFEFFzVMBRxh8xU+C2FwdThwHekmmuwDl6BfPG8OjbiCnRC76CrCWn1 eZ2KqA57liUF9/keIXelOatAQ5YxFOoyp2MZgEY3CV1QqHdjE9YFIRBJo9VZTqiLTLPz l+3V3Z2/BPXfEIUyzaUSzjuqt77HQ0fC8isKADwiqALxGW8WsrX5DDuriItmbXGxI28s gJSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4XL67LggwPcaSb5QCcWsfj8MUU09ylvqAEtbdq8REkw=; b=QV8LbgXFP+grWM6jqu/RN+g3NKpLl/oIhRQglhE7N28dL7DerPf5IQDrI/nPq5LI3Y lHX+WlNU2iX4SFeLHYIih8GzcYLzBQMLY1kPM2MPVRjc8rq6vyB/EMoM4/TIExlssgIy BcUTDHbbj+IBYHY3gSAnGa3+HC2krCpNy2rNTiQh+Tn2tLnStiQgSQ6/0cQsp0/RM32l o4Yel8llBYkAkfjAFqAEBz/QPwsmbrJ8NTIQ1dqdd3WAOLioTJVBdTKpcmgONHIpUeMP I2X60z/U/EA3UdPnKyQ6RUQOrREN7Hv0VBu5BfvNGjxqen/XoZMm6bNWPQ0UH4axcCb+ Tf0g== X-Gm-Message-State: AOAM533vMbsylUUOpABEMca77nitxDGoK1ywuD7he6LEU1XYtPBTPQnq HlKr+oppOTXRv60LgHzH5oZKlxvwp1g= X-Google-Smtp-Source: ABdhPJy9Qp4vEN2eG4L4Wku153PDwwiONhGLC3ikrRcj9kAAu1rFmoeTPpCkCX0YE7SX5vaLsCIghQ== X-Received: by 2002:ac2:4d2e:: with SMTP id h14mr4129764lfk.541.1600092296408; Mon, 14 Sep 2020 07:04:56 -0700 (PDT) Received: from localhost.localdomain (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id d6sm3355904lfb.177.2020.09.14.07.04.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Sep 2020 07:04:55 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: openwrt-devel@lists.openwrt.org Subject: [PATCH uhttpd] ubus: fix blob_buf initialization Date: Mon, 14 Sep 2020 16:04:13 +0200 Message-Id: <20200914140413.14084-1-zajec5@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200914_100500_176132_4F0502F2 X-CRM114-Status: GOOD ( 12.86 ) X-Spam-Score: 0.1 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (0.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:142 listed in] [list.dnswl.org] 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit [zajec5[at]gmail.com] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [zajec5[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Jo-Philipp Wich Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Rafał Miłecki Initializing buffer in the uh_ubus_handle_request() didn't handle batched requests correctly. It resulted in reusing buffer and generating malformed replies. Call blob_buf_init() before every usage of the global buf variable. While at it make two functions take blob_buf pointer as argument: 1. uh_ubus_send_response() 2. uh_ubus_init_json_rpc_response() This helps following global "buf" variable usage and will help avoiding similar bugs in the future. Fixes: 628341fae412 ("ubus: use local "blob_buf" in uh_ubus_handle_request_object()") Signed-off-by: Rafał Miłecki --- ubus.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/ubus.c b/ubus.c index c22e07a..a458f4c 100644 --- a/ubus.c +++ b/ubus.c @@ -169,7 +169,7 @@ static void uh_ubus_send_header(struct client *cl) ustream_printf(cl->us, "\r\n"); } -static void uh_ubus_send_response(struct client *cl) +static void uh_ubus_send_response(struct client *cl, struct blob_buf *buf) { struct dispatch_ubus *du = &cl->dispatch.ubus; const char *sep = ""; @@ -178,7 +178,7 @@ static void uh_ubus_send_response(struct client *cl) if (du->array && du->array_idx > 1) sep = ","; - str = blobmsg_format_json(buf.head, true); + str = blobmsg_format_json(buf->head, true); ops->chunk_printf(cl, "%s%s", sep, str); free(str); @@ -189,32 +189,34 @@ static void uh_ubus_send_response(struct client *cl) return ops->request_done(cl); } -static void uh_ubus_init_json_rpc_response(struct client *cl) +static void uh_ubus_init_json_rpc_response(struct client *cl, struct blob_buf *buf) { struct dispatch_ubus *du = &cl->dispatch.ubus; struct json_object *obj = du->jsobj_cur, *obj2 = NULL; - blobmsg_add_string(&buf, "jsonrpc", "2.0"); + blobmsg_add_string(buf, "jsonrpc", "2.0"); if (obj) json_object_object_get_ex(obj, "id", &obj2); if (obj2) - blobmsg_add_json_element(&buf, "id", obj2); + blobmsg_add_json_element(buf, "id", obj2); else - blobmsg_add_field(&buf, BLOBMSG_TYPE_UNSPEC, "id", NULL, 0); + blobmsg_add_field(buf, BLOBMSG_TYPE_UNSPEC, "id", NULL, 0); } static void uh_ubus_json_rpc_error(struct client *cl, enum rpc_error type) { void *c; - uh_ubus_init_json_rpc_response(cl); + blob_buf_init(&buf, 0); + + uh_ubus_init_json_rpc_response(cl, &buf); c = blobmsg_open_table(&buf, "error"); blobmsg_add_u32(&buf, "code", json_errors[type].code); blobmsg_add_string(&buf, "message", json_errors[type].msg); blobmsg_close_table(&buf, c); - uh_ubus_send_response(cl); + uh_ubus_send_response(cl, &buf); } static void @@ -234,14 +236,16 @@ uh_ubus_request_cb(struct ubus_request *req, int ret) void *r; int rem; + blob_buf_init(&buf, 0); + uloop_timeout_cancel(&du->timeout); - uh_ubus_init_json_rpc_response(cl); + uh_ubus_init_json_rpc_response(cl, &buf); r = blobmsg_open_array(&buf, "result"); blobmsg_add_u32(&buf, "", ret); blob_for_each_attr(cur, du->buf.head, rem) blobmsg_add_blob(&buf, cur); blobmsg_close_array(&buf, r); - uh_ubus_send_response(cl); + uh_ubus_send_response(cl, &buf); } static void @@ -401,9 +405,10 @@ static void uh_ubus_send_list(struct client *cl, struct blob_attr *params) uh_client_unref(cl); - uh_ubus_init_json_rpc_response(cl); + blob_buf_init(&buf, 0); + uh_ubus_init_json_rpc_response(cl, &buf); blobmsg_add_blob(&buf, blob_data(data.buf->head)); - uh_ubus_send_response(cl); + uh_ubus_send_response(cl, &buf); } static bool parse_json_rpc(struct rpc_data *d, struct blob_attr *data) @@ -627,8 +632,6 @@ static void uh_ubus_handle_request(struct client *cl, char *url, struct path_inf { struct dispatch *d = &cl->dispatch; - blob_buf_init(&buf, 0); - switch (cl->request.method) { case UH_HTTP_MSG_POST: