Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nQ89N-0001C7-As for mharc-grub-devel@gnu.org; Fri, 04 Mar 2022 08:39:17 -0500 Received: from eggs.gnu.org ([209.51.188.92]:55996) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nQ89L-00014y-Li for grub-devel@gnu.org; Fri, 04 Mar 2022 08:39:15 -0500 Received: from [2607:f8b0:4864:20::1032] (port=35786 helo=mail-pj1-x1032.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nQ89J-0000WH-Pb for grub-devel@gnu.org; Fri, 04 Mar 2022 08:39:15 -0500 Received: by mail-pj1-x1032.google.com with SMTP id mg21-20020a17090b371500b001bef9e4657cso7270793pjb.0 for ; Fri, 04 Mar 2022 05:39:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=W0jrdAlUEpZPbO/muDyqlRHaTgDD4YYHSYkrwmUzlqc=; b=kQz4lIb+26EH8Xs7xBuTcMMmkZbow63v7P9IQjf9O/n0aPjhUoHXiChj7r2ik9nDWy TE0Kr3a4OJ99NIbYVxwXP5bubpXhPjXwGYRe4oyVPVkrRllHFfGxsdqSnwqYzqQV86RJ lw+AG6YP5VHFK0Y5c3p5nRUWY1KPX4jsGMnDs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=W0jrdAlUEpZPbO/muDyqlRHaTgDD4YYHSYkrwmUzlqc=; b=6zpjdFIPjpBRiu6Am96fH2AWa08CgdGpH9dOlL4tJZIl+ALRJO/UCpf6kYPdzUAV+9 m3q2N2GXrVC9TePe26NJY28OxoY6t13zncc/qiBvu2pt2SsaVRbHZsEokWRWegyjSuis C0B5pqEQCFxZtUyKJyuBxrP0ArxQmQjQeKAeBhoHEX5EpQUMCfeKsne34h5bq1RUXt/X iBJ0IVM5xrV3Q3C6B/n7H1hkVNQL7M7W8ktWQEVGSlXvVRdr/fAsPKcE5tFRirLD+2HV g6UFKFHCwvKopSmisLFFDbtoRt7XRrXeR7yR1cb1LiWHMgkZrAnmDr6CY3KY6Koylcc7 kC6Q== X-Gm-Message-State: AOAM533kkQ5DgWIxae8cPlmZJ1FqzLbYIfVqIpIcEUUfPhOcks8Sc4Yg /tKjPxG6Y3ojp3/4uG8S8kfjOeqZXNP3HA== X-Google-Smtp-Source: ABdhPJzd6816do1mY8T+z7lLWCaRL1ebEmfifz7Ds21ynKIcG+UYemXlHae2XPmJJhOow9A5qcn4xQ== X-Received: by 2002:a17:90a:1188:b0:1bd:36d0:d7b2 with SMTP id e8-20020a17090a118800b001bd36d0d7b2mr10472883pja.223.1646401151344; Fri, 04 Mar 2022 05:39:11 -0800 (PST) Received: from localhost ([2001:4479:e300:600:9438:733d:bf7e:4b64]) by smtp.gmail.com with ESMTPSA id c12-20020a621c0c000000b004f6ada49d83sm3116199pfc.73.2022.03.04.05.39.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 05:39:10 -0800 (PST) From: Daniel Axtens To: grub-devel@gnu.org Cc: Daniel Axtens Subject: [PATCH] net: check against nb->tail in grub_netbuff_pull Date: Sat, 5 Mar 2022 00:39:04 +1100 Message-Id: <20220304133904.2562231-1-dja@axtens.net> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::1032 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=dja@axtens.net; helo=mail-pj1-x1032.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Mar 2022 13:39:16 -0000 GRUB netbuff structure members track 2 different things: the extent of memory allocated for the packet, and the extent of memory currently being worked on. This works out in the structure as follows: nb->head: beginning of the allocation nb->data: beginning of the working data nb->tail: end of the working data nb->end: end of the allocation The head and end pointers are set in grub_netbuff_alloc and do not change. The data and tail pointers are initialised to point at start of the allocation (that is, head == data == tail initially), and are then manipulated by grub_netbuff_* functions. Key functions are as follows: - grub_netbuff_put: 'Put' more data into the packet - advance nb->tail. - grub_netbuff_unput: trim the tail of the packet - retract nb->tail - grub_netbuff_pull: 'consume' some packet data - advance nb->data - grub_netbuff_reserve: reserve space for future headers - advance nb->data and nb->tail - grub_netbuff_push: 'un-consume' data to allow headers to be written - retract nb->data. Each of those functions does some form of error checking. For example, grub_netbuff_put does not allow nb->tail to exceed nb->end, and grub_netbuff_push does not allow nb->data to be before nb->head. However, grub_netbuff_pull's error checking is a bit weird. It advances nb->data and checks that it does not exceed nb->end. That allows you to get into the situation where nb->data > nb->tail, which should not be. Make grub_netbuff_pull check against both nb->tail and nb->end. In theory just checking against ->tail should be sufficient but the extra check should be cheap and seems like good defensive practice. Signed-off-by: Daniel Axtens --- I'm not aware of any particular bug this fixes. All it can do is prevent you reading uninitialised but still allocated memory. It just seems like a good idea. The netboot test still passses on the pc platform, more testing would be good. --- grub-core/net/netbuff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grub-core/net/netbuff.c b/grub-core/net/netbuff.c index dbeeefe4783c..72e5296356f0 100644 --- a/grub-core/net/netbuff.c +++ b/grub-core/net/netbuff.c @@ -54,7 +54,7 @@ grub_err_t grub_netbuff_pull (struct grub_net_buff *nb, grub_size_t len) { nb->data += len; - if (nb->data > nb->end) + if (nb->data > nb->end || nb->data > nb->tail) return grub_error (GRUB_ERR_BUG, "pull out of the packet range."); return GRUB_ERR_NONE; -- 2.32.0