aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tests / test_netboot_text.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* test_netboot_text.c -- nb_text_len (native/boot/netboot_proto.h), the |
| 18 | * length of the DONE text NETLDR.EFI sends. Negative fixture for the CodeQL |
| 19 | * alert "array offset used before range check" (net_loader.c done()): a text |
| 20 | * of exactly NB_TEXT_MAX bytes with no NUL sits at the end of a heap block, |
| 21 | * so reading text[NB_TEXT_MAX] is a heap overflow that the sanitize build |
| 22 | * (make sanitize, ASan) reports. */ |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "ck_test.h" |
| 27 | #include "netboot_proto.h" |
| 28 | |
| 29 | static char *block(size_t n, size_t nul_at) |
| 30 | { |
| 31 | char *p = malloc(n); |
| 32 | if (!p) |
| 33 | abort(); |
| 34 | memset(p, 'A', n); |
| 35 | if (nul_at < n) |
| 36 | p[nul_at] = 0; |
| 37 | return p; |
| 38 | } |
| 39 | |
| 40 | int main(void) |
| 41 | { |
| 42 | char *p = block(NB_TEXT_MAX, (size_t)-1); /* unterminated, exactly at the cap */ |
| 43 | CHECK(nb_text_len(p) == NB_TEXT_MAX); |
| 44 | free(p); |
| 45 | p = block(NB_TEXT_MAX + 40, (size_t)-1); /* unterminated, longer than the cap */ |
| 46 | CHECK(nb_text_len(p) == NB_TEXT_MAX); |
| 47 | free(p); |
| 48 | p = block(NB_TEXT_MAX, NB_TEXT_MAX - 1); /* NUL in the last byte */ |
| 49 | CHECK(nb_text_len(p) == NB_TEXT_MAX - 1); |
| 50 | free(p); |
| 51 | p = block(1, 0); |
| 52 | CHECK(nb_text_len(p) == 0); |
| 53 | free(p); |
| 54 | CHECK(nb_text_len("verified, starting") == 18); |
| 55 | return ck_t_verdict("test_netboot_text"); |
| 56 | } |