project/libubox.git
4 days agoscripts: devel-build: add missing LUAPATH master
Álvaro Fernández Rojas [Tue, 18 Nov 2025 09:36:26 +0000 (10:36 +0100)]
scripts: devel-build: add missing LUAPATH

Add missing LUA_PATH, which prevents installing `uloop.so` to
`/usr/local/lib/lua/5.1`.
Fixes https://github.com/openwrt/libubox/issues/36.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
4 days agogithub: ci: add MIPS64, PowerPC64 and RISCV64
Álvaro Fernández Rojas [Tue, 18 Nov 2025 07:51:56 +0000 (08:51 +0100)]
github: ci: add MIPS64, PowerPC64 and RISCV64

MIPS64, PowerPC64 and RISCV64 are popular OpenWrt archs.
Refactor the sizes build step to generate the table programatically.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
5 days agotests: shunit2: add test for _jshn_append via JSON script
Petr Štetiar [Thu, 13 Nov 2025 11:56:15 +0000 (11:56 +0000)]
tests: shunit2: add test for _jshn_append via JSON script

Add another test which verifies _jshn_append leading space fix in commit
82cb5fd66af9 ("libubox: Drop extraneous space when appending values to
variable") by appending keys to JSON objects, making sure there is no
leading space on the first key when adding second key.

  test_jshn_append_via_json_script
  ASSERT:expected:<first> but was:< first>
  ASSERT:expected:<first second> but was:< first second>

Tests: #16
Signed-off-by: Petr Štetiar <ynezz@true.cz>
Link: https://github.com/openwrt/libubox/pull/30
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
5 days agotests: shunit2: add dump and setters coverage
Philip Prindeville [Mon, 17 Nov 2025 17:10:40 +0000 (10:10 -0700)]
tests: shunit2: add dump and setters coverage

Exercise json_dump and json_add_* functions.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Link: https://github.com/openwrt/libubox/pull/35
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
5 days agobuild: install libubox
Álvaro Fernández Rojas [Mon, 17 Nov 2025 11:02:30 +0000 (12:02 +0100)]
build: install libubox

Install libubox and properly use it by adding it to PATH and LD_LIBRARY_PATH.
This is needed for tests using `jshn` which are executed with devel-build.sh
and Github CI.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
Link: https://github.com/openwrt/libubox/pull/34
8 days agoblobmsg: refactor blobmsg_cast_u64/s64
Álvaro Fernández Rojas [Mon, 10 Nov 2025 16:36:59 +0000 (17:36 +0100)]
blobmsg: refactor blobmsg_cast_u64/s64

Instead of calling blobmsg_type() for each if/else block, just call it
once and use it with a switch.

Link: https://github.com/openwrt/libubox/pull/24
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
8 days agogithub: ci: minor fixes
Álvaro Fernández Rojas [Fri, 14 Nov 2025 13:55:51 +0000 (14:55 +0100)]
github: ci: minor fixes

- Fix size_powerpc_basic_sa -> size_powerpc_basic_a typo.
- Refactor cmake lines.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
8 days agogithub: ci: add powerpc arch
Álvaro Fernández Rojas [Fri, 14 Nov 2025 10:55:19 +0000 (11:55 +0100)]
github: ci: add powerpc arch

PowerPC is another popular OpenWrt arch.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
8 days agogithub: ci: add cmake build and source directories
Álvaro Fernández Rojas [Fri, 14 Nov 2025 09:12:12 +0000 (10:12 +0100)]
github: ci: add cmake build and source directories

Add cmake build and source directories to suppress the following warning:
CMake Warning:
  No source or binary directory provided. Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
8 days agogithub: ci: tests: rename test step
Álvaro Fernández Rojas [Fri, 14 Nov 2025 09:06:43 +0000 (10:06 +0100)]
github: ci: tests: rename test step

Rename "Execute tests" step to "Test libubox".

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
8 days agogithub: ci: tests: add build/bin to path
Álvaro Fernández Rojas [Fri, 14 Nov 2025 09:05:58 +0000 (10:05 +0100)]
github: ci: tests: add build/bin to path

Add build/bin to GH environment path.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
8 days agogithub: ci: disable json-c tests
Álvaro Fernández Rojas [Fri, 14 Nov 2025 07:36:51 +0000 (08:36 +0100)]
github: ci: disable json-c tests

Disable BUILD_TESTING to save time when building json-c.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
8 days agoscripts: devel-build: disable json-c tests
Álvaro Fernández Rojas [Fri, 14 Nov 2025 07:35:18 +0000 (08:35 +0100)]
scripts: devel-build: disable json-c tests

Disable BUILD_TESTING to save time when building json-c.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
9 days agotests: shunit2: add test for _jshn_append leading space fix
Petr Štetiar [Thu, 13 Nov 2025 10:38:41 +0000 (10:38 +0000)]
tests: shunit2: add test for _jshn_append leading space fix

Verifies the fix in commit 2065c3909935 ("libubox: Drop extraneous space
when appending values to variable") that prevents leading spaces when
appending to empty variables.

 test_jshn_append_no_leading_space
 ASSERT:expected:<foo> but was:< foo>
 ASSERT:expected:<first second> but was:< first second>

References: #16
Signed-off-by: Petr Štetiar <ynezz@true.cz>
9 days agolibubox: Drop extraneous space when appending values to variable
Felix Fietkau [Fri, 15 Sep 2023 22:00:00 +0000 (16:00 -0600)]
libubox: Drop extraneous space when appending values to variable

Don't have a leading space when building out a variable via appends.

Fixes: #15
Signed-off-by: Felix Fietkau <nbd@nbd.name>
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Signed-off-by: Petr Štetiar <ynezz@true.cz> [fix fixes trailer]
11 days agolibubox: add a simple build script
Álvaro Fernández Rojas [Mon, 10 Nov 2025 21:45:20 +0000 (22:45 +0100)]
libubox: add a simple build script

Should make it a little bit easier for people who want to contribute to
libubox.

Link: https://github.com/openwrt/libubox/pull/26
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
11 days agogithub: ci: add tests
Álvaro Fernández Rojas [Tue, 11 Nov 2025 07:47:54 +0000 (08:47 +0100)]
github: ci: add tests

Build libubox with UNIT_TESTING and execute tests.

Link: https://github.com/openwrt/libubox/pull/27
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
2 weeks agogithub: fix CI apt dependencies
Álvaro Fernández Rojas [Mon, 3 Nov 2025 06:38:52 +0000 (07:38 +0100)]
github: fix CI apt dependencies

We need to run 'apt update' before installing the APT packages.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
4 weeks agogithub: improve CI build
Álvaro Fernández Rojas [Sun, 12 Oct 2025 18:55:48 +0000 (20:55 +0200)]
github: improve CI build

Add Github CI supporting different architectures and libubox build options.
Add summary with binary sizes.
Upload binaries as artifacts.
Add OpenWrt formalities.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
5 weeks agoadd debian/ directory
Felix Fietkau [Tue, 14 Oct 2025 08:11:51 +0000 (08:11 +0000)]
add debian/ directory

Support building debian packages

Signed-off-by: Felix Fietkau <nbd@nbd.name>
6 weeks agoexamples: CMakeLists: drop redundant cmake_minimum_required
David Härdeman [Sun, 5 Oct 2025 15:49:28 +0000 (17:49 +0200)]
examples: CMakeLists: drop redundant cmake_minimum_required

The examples depend on the parent libubox project and can't be built
independently.

Drop redundant cmake_minimum_required from examples/CMakeLists.txt
and inherit the version from the parent CMake project to keep
version consistency.

Signed-off-by: David Härdeman <david@hardeman.nu>
Link: https://github.com/openwrt/libubox/pull/21
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
6 weeks agolua: CMakeLists: drop redundant cmake_minimum_required
David Härdeman [Sun, 5 Oct 2025 15:48:37 +0000 (17:48 +0200)]
lua: CMakeLists: drop redundant cmake_minimum_required

The Lua module depends on the parent libubox project and can't be built
independently.

Drop redundant cmake_minimum_required from lua/CMakeLists.txt and
inherit the version from the parent CMake project to keep version
consistency.

Signed-off-by: David Härdeman <david@hardeman.nu>
Link: https://github.com/openwrt/libubox/pull/21
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
7 weeks agolua: build: require CMake >= 3.10 due to dropped legacy support
Hauke Mehrtens [Fri, 3 Oct 2025 17:44:37 +0000 (19:44 +0200)]
lua: build: require CMake >= 3.10 due to dropped legacy support

CMake version 4.0 and later require minimum version of 3.5 or later.
Update to minimum version 3.10 which is the last not deprecated minimum
version.

CMake 3.10 was released in November 2017 and is included in Ubuntu 18.04.

Suggested-by: Hannu Nyman <hannu.nyman@iki.fi>
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
7 weeks agoexamples: CMakeLists: update cmake minimum required version to 3.10
Christian Marangi [Fri, 3 Oct 2025 22:05:58 +0000 (00:05 +0200)]
examples: CMakeLists: update cmake minimum required version to 3.10

New cmake version 4.0 requires at least 3.5 version as the minimum
required version with it increased to 3.10 in to-be-released cmake
versions.

Set the minimum required version to 3.10 to future-proof for future
cmake version.

Suggested-by: Hannu Nyman <hannu.nyman@iki.fi>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
4 months agoudebug: fix issue with snapshot of remote ring
Felix Fietkau [Wed, 23 Jul 2025 09:49:27 +0000 (11:49 +0200)]
udebug: fix issue with snapshot of remote ring

Avoid skipping over the current ring head

Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 months agoudebug: remove obsolete debug message
Felix Fietkau [Tue, 22 Jul 2025 11:03:43 +0000 (13:03 +0200)]
udebug: remove obsolete debug message

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 months agoremove compatibility code for older json-c versions
Rosen Penev [Thu, 3 Feb 2022 22:42:26 +0000 (14:42 -0800)]
remove compatibility code for older json-c versions

Avoid accidentally attempting to use the incompatible jsoncpp library

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
11 months agoblob: constify attr argument to blob_memdup
Felix Fietkau [Thu, 19 Dec 2024 10:26:18 +0000 (11:26 +0100)]
blob: constify attr argument to blob_memdup

It is not modified, so it can be const

Signed-off-by: Felix Fietkau <nbd@nbd.name>
19 months agoustream: prevent recursive calls to the read callback
Felix Fietkau [Fri, 29 Mar 2024 09:23:28 +0000 (10:23 +0100)]
ustream: prevent recursive calls to the read callback

Simplifies stacked ustreams and calling poll from the read function.
Reuse an unused leftover struct member in order to not break ABI.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
20 months agoCI: add CodeQL workflow tests
Christian Marangi [Fri, 23 Feb 2024 23:44:12 +0000 (00:44 +0100)]
CI: add CodeQL workflow tests

Add CodeQL workflow action for security testing.

Enable security-and-quality queries.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
20 months agoCI: add build test run
Christian Marangi [Fri, 23 Feb 2024 23:14:21 +0000 (00:14 +0100)]
CI: add build test run

Add build test run and fuzzing test. Lua support is also enabled.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
21 months agoudebug: fix crash in udebug_entry_vprintf with longer strings
Felix Fietkau [Fri, 26 Jan 2024 20:00:14 +0000 (21:00 +0100)]
udebug: fix crash in udebug_entry_vprintf with longer strings

The passed va_list ap cannot be used more than once. In order to deal with
vsprintf retry, it needs to be copied first. Fixes a procd crash observed
on several platforms.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoCMakeLists.txt: bump minimum cmake version
Felix Fietkau [Mon, 18 Dec 2023 12:31:23 +0000 (13:31 +0100)]
CMakeLists.txt: bump minimum cmake version

Removes warnings and fixes rpath issues on macOS

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoudebug: fix file descriptor initialization for __udebug_buf_map
Felix Fietkau [Mon, 4 Dec 2023 13:37:31 +0000 (14:37 +0100)]
udebug: fix file descriptor initialization for __udebug_buf_map

Pass the fd to __udebug_buf_map, set buf->fd only if mapping worked

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoudebug: add mips specific quirk
Felix Fietkau [Mon, 4 Dec 2023 12:26:07 +0000 (13:26 +0100)]
udebug: add mips specific quirk

On some MIPS systems, mmap addresses need to be aligned to multiple pages, in
order to avoid issues with data cache aliases. Add an arch specific quirk to
allocate memory in 32 KiB chunks and align addresses returned by mmap.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoudebug: add more checks for uninitialized buffers
Felix Fietkau [Mon, 4 Dec 2023 11:02:52 +0000 (12:02 +0100)]
udebug: add more checks for uninitialized buffers

Simplifies and fixes error handling

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoudebug: wait for response after buffer add/remove
Felix Fietkau [Thu, 30 Nov 2023 11:23:30 +0000 (12:23 +0100)]
udebug: wait for response after buffer add/remove

Fixes a race condition where freeing a buffer and immediately re-allocating and
adding it would fail to pass the file descriptor to udebugd

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoudebug: add inline helper function to test if a buffer is allocated
Felix Fietkau [Wed, 29 Nov 2023 21:00:19 +0000 (22:00 +0100)]
udebug: add inline helper function to test if a buffer is allocated

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoudebug: add functions for manipulating entry length
Felix Fietkau [Wed, 29 Nov 2023 14:38:30 +0000 (15:38 +0100)]
udebug: add functions for manipulating entry length

Can be used to reserve worst case length using udebug_entry_append,
then setting the final length using udebug_entry_set_length

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agolink librt if needed for shm_open
Felix Fietkau [Tue, 28 Nov 2023 21:09:24 +0000 (22:09 +0100)]
link librt if needed for shm_open

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoudebug: add ulog support
John Crispin [Tue, 28 Nov 2023 07:13:49 +0000 (08:13 +0100)]
udebug: add ulog support

Make ulog able to also log via udebug.

Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agouloop: fix build using C++ compilers
Felix Fietkau [Tue, 28 Nov 2023 09:54:53 +0000 (10:54 +0100)]
uloop: fix build using C++ compilers

Rename the 'private' field to 'priv' in order to avoid using a C++ keyword

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agoudebug: add udebug library code
Felix Fietkau [Mon, 27 Nov 2023 14:44:33 +0000 (15:44 +0100)]
udebug: add udebug library code

Copied and adapted from udebug.git

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agouloop: reset flags after __uloop_fd_delete call
Felix Fietkau [Mon, 27 Nov 2023 13:59:06 +0000 (14:59 +0100)]
uloop: reset flags after __uloop_fd_delete call

Fixes fd delete with kqueue, which relies on the previous flags value

Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 months agouloop: fix typo in signal handling rework
Felix Fietkau [Mon, 27 Nov 2023 17:29:43 +0000 (18:29 +0100)]
uloop: fix typo in signal handling rework

Fixes procd issues

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2 years agouloop: properly initialize signal handler mask
Jo-Philipp Wich [Fri, 3 Nov 2023 21:24:04 +0000 (22:24 +0100)]
uloop: properly initialize signal handler mask

The structure passed to `sigaction()` left it's `sa_mask` member uninitialized.

Fixes: beb356b ("uloop: add support for user defined signal handlers")
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2 years agouloop: add support for user defined signal handlers
Jo-Philipp Wich [Mon, 16 Oct 2023 14:35:28 +0000 (16:35 +0200)]
uloop: add support for user defined signal handlers

Reuse and extend the existing signal waker pipe mechanism to add user
defined signal handling functionality to uloop.

This commit introduces two new api functions `uloop_signal_add()` and
`uloop_signal_remove()` along with a new structure type `uloop_signal`
to allow adding and removing arbitrary signal handlers.

Registered signal handlers are maintained in a linked list and matched
by their signo member value which allows registering multiple handlers
for the same signal numbers.

Upon registering a new signal handler, the existing handler is saved
in the `uloop_signal` structure. When removing the user defined signal
handler, the original behavior is restored.

The Lua binding has been updated as well to support the new signal
handler mechanism.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2 years agouloop: add support for interval timers
Jo-Philipp Wich [Sat, 14 Oct 2023 22:17:36 +0000 (00:17 +0200)]
uloop: add support for interval timers

So far, the only way to implement periodic interval timers was to use
one-shot uloop_timeout timers which are rearmed within their completion
callback immediately on expiration.

While simple, this approach is not very precise and interval lengths will
slowly drift over time, due to callback execution overhead, scheduling
granularity etc.

In order to make uloop provide stable and precise interval timer
capabilities, this commit introduces a new `uloop_interval` structure
along with the new related `uloop_interval_set()`, `uloop_interval_cancel()`
and `uloop_interval_remaining()` api functions.

Periodic timers are implemented using the timerfd facility an Linux and
kqueue EVFILT_TIMER events on macOS/BSD.

The Lua binding has been updated to include support for the new timer type
as well.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2 years agouloop: add support for integrating with a different event loop
Felix Fietkau [Tue, 23 May 2023 10:24:33 +0000 (12:24 +0200)]
uloop: add support for integrating with a different event loop

- support reading the next timeout in order to determine the poll timeout
- add a callback for fd add/delete/update

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2 years agouloop: fix uloop_run_timeout
Felix Fietkau [Tue, 23 May 2023 13:09:33 +0000 (15:09 +0200)]
uloop: fix uloop_run_timeout

Avoid running infinite poll loop, fix timeout value

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2 years agoblobmsg: Don't do at run-time what can be done at compile-time
Philip Prindeville [Fri, 14 Apr 2023 18:37:06 +0000 (12:37 -0600)]
blobmsg: Don't do at run-time what can be done at compile-time

Repeatedly calling a run-time function like strlen() on an
invariant value is inefficient, especially if that value can be
computed once (at initialization) or better yet, computed at
compile-time.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
2 years agojshn.sh: Add pretty-printing to json_dump
Philip Prindeville [Fri, 14 Apr 2023 18:37:05 +0000 (12:37 -0600)]
jshn.sh: Add pretty-printing to json_dump

If a JSON file might be read by a human, say for debugging, it
could be useful to pretty-print it.  We do this in places by
calling "json_dump -i" but it shouldn't be necessary to know the
arguments to "jshn" (and indeed, that's not portable if we retool
the underlying implementation). Conversely output that's ephemeral
doesn't need to be pretty (say being piped as input to another
command).

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
2 years agousock: fix poll return code check
Felix Fietkau [Wed, 8 Mar 2023 08:38:53 +0000 (09:38 +0100)]
usock: fix poll return code check

errno needs to be compared against EINTR/EAGAIN instead of the return code,
and only if the return code is < 0.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2 years agoblobmsg: add blobmsg_parse_array_attr
Felix Fietkau [Tue, 3 Jan 2023 09:43:44 +0000 (10:43 +0100)]
blobmsg: add blobmsg_parse_array_attr

Wrapper around blobmsg_parse_array, similar to blobmsg_parse_attr

Signed-off-by: Felix Fietkau <nbd@nbd.name>
3 years agoblobmsg: add blobmsg_parse_attr function
Felix Fietkau [Wed, 23 Nov 2022 11:29:19 +0000 (12:29 +0100)]
blobmsg: add blobmsg_parse_attr function

This allows turning the common pattern of:
  blobmsg_parse(policy, ARRAY_SIZE(policy), tb, blobmsg_data(data), blobmsg_len(data));

into:
  blobmsg_parse_attr(policy, ARRAY_SIZE(policy), tb, data);

Signed-off-by: Felix Fietkau <nbd@nbd.name>
3 years agojshn.sh: add json_add_fields function for adding multiple fields at once
Felix Fietkau [Tue, 27 Sep 2022 12:17:51 +0000 (14:17 +0200)]
jshn.sh: add json_add_fields function for adding multiple fields at once

This simplifies passing extra object data as a function parameter

Signed-off-by: Felix Fietkau <nbd@nbd.name>
3 years agoblobmsg: work around false positive gcc -Warray-bounds warnings
Felix Fietkau [Sun, 15 May 2022 11:42:56 +0000 (13:42 +0200)]
blobmsg: work around false positive gcc -Warray-bounds warnings

Using the return value of blobmsg_name as input argument to strcpy can lead
to warnings like these:

error: 'strcpy' offset 6 from the object at 'cur' is out of the bounds of referenced subobject 'name' with type 'uint8_t[]' {aka 'unsigned char[]'} at offset 6 [-Werror=array-bounds]

Fix this by replacing hdr->name with the equivalent hdr + 1

Signed-off-by: Felix Fietkau <nbd@nbd.name>
3 years agoblobmsg: implicitly reserve space for 0-terminator in string buf alloc
Felix Fietkau [Thu, 12 May 2022 11:22:56 +0000 (13:22 +0200)]
blobmsg: implicitly reserve space for 0-terminator in string buf alloc

It may not be clear to all users of this API if the provided maxlen argument
refers to the maximum string length or the maximum buffer size.
In order to improve safety and convenience of this API, make it refer to
the maximum string length.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
3 years agolist.h: add container_of_safe macro
Felix Fietkau [Fri, 29 Apr 2022 10:57:25 +0000 (12:57 +0200)]
list.h: add container_of_safe macro

It works like container_of, except that it also deals with NULL pointers

Signed-off-by: Felix Fietkau <nbd@nbd.name>
3 years agoblob: clear buf->head when freeing a buffer
Felix Fietkau [Thu, 10 Feb 2022 20:02:16 +0000 (21:02 +0100)]
blob: clear buf->head when freeing a buffer

Prevents accidental silent use-after-free bugs

Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 years agovlist: define vlist_for_each_element_safe
Daniel Golle [Fri, 12 Nov 2021 20:04:29 +0000 (20:04 +0000)]
vlist: define vlist_for_each_element_safe

Yet another macro wrapper around the corresponding avl_* macro.
This new macro makes it possible to iterate over vlists in ways which
may have destructive consequences without being punished by segfault.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
4 years agouloop: deprecate uloop_timeout_remaining
Stijn Tintel [Thu, 4 Nov 2021 10:28:39 +0000 (12:28 +0200)]
uloop: deprecate uloop_timeout_remaining

We have uloop_timeout_remaining64 now.

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: Jo-Philipp Wich <jo@mein.io>
Acked-by: John Crispin <john@phrozen.org>
4 years agolua/uloop: use uloop_timeout_remaining64
Stijn Tintel [Thu, 4 Nov 2021 10:31:28 +0000 (12:31 +0200)]
lua/uloop: use uloop_timeout_remaining64

We will deprecate uloop_timeout_remaining soon.

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: Jo-Philipp Wich <jo@mein.io>
Acked-by: John Crispin <john@phrozen.org>
4 years agouloop: add uloop_timeout_remaining64
Stijn Tintel [Thu, 4 Nov 2021 10:26:41 +0000 (12:26 +0200)]
uloop: add uloop_timeout_remaining64

This uses the same return type as tv_diff so we don't need to check for
integer overflow.

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: Jo-Philipp Wich <jo@mein.io>
Acked-by: John Crispin <john@phrozen.org>
4 years agouloop: restore return type of uloop_timeout_remaining
Stijn Tintel [Thu, 4 Nov 2021 10:14:02 +0000 (12:14 +0200)]
uloop: restore return type of uloop_timeout_remaining

The uloop_timeout_remaining function is public and changing its return
type breaks ABI. Change the return type back to int, and return INT_MIN
or INT_MAX if the value returned by tv_diff would overflow integer.

Fixes: be3dc7223a6d ("uloop: avoid integer overflow in tv_diff")
Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: Jo-Philipp Wich <jo@mein.io>
Acked-by: John Crispin <john@phrozen.org>
4 years agouloop: avoid integer overflow in tv_diff
Stijn Tintel [Wed, 3 Nov 2021 23:17:39 +0000 (01:17 +0200)]
uloop: avoid integer overflow in tv_diff

The tv_diff function can potentially overflow as soon as t2->tv_sec is
larger than 2147483. This is very easily hit in ujail, after only
2147484 seconds of uptime, or 24.85 days.

Improve the behaviour by changing the return type to int64_t.

Fixes: FS#3943
Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
4 years agolist.h: add a few missing iterator macros
Felix Fietkau [Thu, 19 Aug 2021 06:47:04 +0000 (08:47 +0200)]
list.h: add a few missing iterator macros

Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 years agojson_script: fix unannotated fall-through warning
Felix Fietkau [Sun, 16 May 2021 16:07:24 +0000 (18:07 +0200)]
json_script: fix unannotated fall-through warning

Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 years agoutils.h: add fallthrough macro
Felix Fietkau [Sun, 16 May 2021 15:31:57 +0000 (17:31 +0200)]
utils.h: add fallthrough macro

This can be used to silence clang warnings about unannotated fall-through

Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 years agoblob: fix exceeding maximum buffer length
Zefir Kurtisi [Fri, 23 Apr 2021 17:48:01 +0000 (19:48 +0200)]
blob: fix exceeding maximum buffer length

Currently there is no measure in place to prevent the blob buffer
to exceed its maximum allowed length of 16MB. Continuously
calling blob_add() will expand the buffer until it exceeds
BLOB_ATTR_LEN_MASK and after that will return valid blob_attr
pointer without increasing the buflen.

A test program was added in the previous commit, this one fixes
the issue by asserting that the new bufflen after grow does not
exceed BLOB_ATTR_LEN_MASK.

Signed-off-by: Zefir Kurtisi <zefir.kurtisi@gmail.com>
4 years agotests: add blob-buffer overflow test
Zefir Kurtisi [Fri, 23 Apr 2021 17:48:00 +0000 (19:48 +0200)]
tests: add blob-buffer overflow test

The blob buffer has no limitation in place
to prevent buflen to exceed maximum size.

This commit adds a test to demonstrate how
a blob increases past the maximum allowd
size of 16MB. It continuously adds chunks
of 64KB and with the 255th one blob_add()
returns a valid attribute pointer but the
blob's buflen does not increase.

The test is used to demonstrate the
failure, which is fixed with a follow-up
commit.

Signed-off-by: Zefir Kurtisi <zefir.kurtisi@gmail.com>
[adjusted test case for cram usage]
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agolibubox: tests: add more blobmsg/json test cases
Peter Seiderer [Sat, 6 Mar 2021 10:54:50 +0000 (11:54 +0100)]
libubox: tests: add more blobmsg/json test cases

* add mixed int/double tests
 * add blobmsg_cast_u64/blobmsg_cast_s64 tests

Signed-off-by: Peter Seiderer <ps.report@gmx.net>
4 years agotests: cram: test_base64: really fix failing tests
Petr Štetiar [Wed, 3 Mar 2021 14:37:52 +0000 (15:37 +0100)]
tests: cram: test_base64: really fix failing tests

Remove the checks for 'Aborted (core dumped)' message altogether as it's
not reliable and not portable.

References: https://gitlab.com/openwrt/project/libubox/-/jobs/1070226897
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agotests: cram: test_base64: fix failing tests
Petr Štetiar [Wed, 3 Mar 2021 12:49:27 +0000 (13:49 +0100)]
tests: cram: test_base64: fix failing tests

Seems like latest version of llvm compiler/sanitizer has changed
behaviour during crash so `Aborted (core dumped)` is now printed to
stdout.

Fixes following issue:

 --- /builds/openwrt/project/libubox/tests/cram/test_base64.t
 +++ /builds/openwrt/project/libubox/tests/cram/test_base64.t.err
 @@ -49,9 +49,7 @@
    b64_encode: Assertion `dest && targsize > 0' failed.

    $ test-b64_decode-san 2> output.log; check
 -  Aborted (core dumped)
    b64_decode: Assertion `dest && targsize > 0' failed.

    $ test-b64_encode-san 2> output.log; check
 -  Aborted (core dumped)
    b64_encode: Assertion `dest && targsize > 0' failed.

References: https://gitlab.com/openwrt/project/libubox/-/jobs/1069840314
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agolibubox: fix BLOBMSG_CAST_INT64 (do not override BLOBMSG_TYPE_DOUBLE)
Peter Seiderer [Fri, 26 Feb 2021 19:24:20 +0000 (20:24 +0100)]
libubox: fix BLOBMSG_CAST_INT64 (do not override BLOBMSG_TYPE_DOUBLE)

Commit 9e52171 ('blobmsg: introduce BLOBMSG_CAST_INT64') broke
blobmsg_parse() for BLOBMSG_TYPE_DOUBLE.

This is because the enum definition leads to the following double
define for BLOBMSG_CAST_INT64/BLOBMSG_TYPE_DOUBLE as value 8.

Tested with:

$ cat test-enum-001.c
  #include <stdio.h>

  enum blobmsg_type {
   BLOBMSG_TYPE_UNSPEC,
   BLOBMSG_TYPE_ARRAY,
   BLOBMSG_TYPE_TABLE,
   BLOBMSG_TYPE_STRING,
   BLOBMSG_TYPE_INT64,
   BLOBMSG_TYPE_INT32,
   BLOBMSG_TYPE_INT16,
   BLOBMSG_TYPE_INT8,
   BLOBMSG_TYPE_DOUBLE,
   __BLOBMSG_TYPE_LAST,
   BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1,
   BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8,
   BLOBMSG_CAST_INT64,
  };

  int main(int artc, char* argv[]) {
   printf("BLOBMSG_TYPE_UNSPEC: %d\n", BLOBMSG_TYPE_UNSPEC);
   printf("BLOBMSG_TYPE_ARRAY: %d\n", BLOBMSG_TYPE_ARRAY);
   printf("BLOBMSG_TYPE_TABLE: %d\n", BLOBMSG_TYPE_TABLE);
   printf("BLOBMSG_TYPE_STRING: %d\n", BLOBMSG_TYPE_STRING);
   printf("BLOBMSG_TYPE_INT64: %d\n", BLOBMSG_TYPE_INT64);
   printf("BLOBMSG_TYPE_INT32: %d\n", BLOBMSG_TYPE_INT32);
   printf("BLOBMSG_TYPE_INT16: %d\n", BLOBMSG_TYPE_INT16);
   printf("BLOBMSG_TYPE_INT8: %d\n", BLOBMSG_TYPE_INT8);
   printf("BLOBMSG_TYPE_DOUBLE: %d\n", BLOBMSG_TYPE_DOUBLE);
   printf("__BLOBMSG_TYPE_LAST: %d\n", __BLOBMSG_TYPE_LAST);
   printf("BLOBMSG_TYPE_LAST: %d\n", BLOBMSG_TYPE_LAST);
   printf("BLOBMSG_TYPE_BOOL: %d\n", BLOBMSG_TYPE_BOOL);
   printf("BLOBMSG_CAST_INT64: %d\n", BLOBMSG_CAST_INT64);
   return 0;
  }

$ gcc test-enum-001.c

$ ./a.out
  BLOBMSG_TYPE_UNSPEC: 0
  BLOBMSG_TYPE_ARRAY: 1
  BLOBMSG_TYPE_TABLE: 2
  BLOBMSG_TYPE_STRING: 3
  BLOBMSG_TYPE_INT64: 4
  BLOBMSG_TYPE_INT32: 5
  BLOBMSG_TYPE_INT16: 6
  BLOBMSG_TYPE_INT8: 7
  BLOBMSG_TYPE_DOUBLE: 8
  __BLOBMSG_TYPE_LAST: 9
  BLOBMSG_TYPE_LAST: 8
  BLOBMSG_TYPE_BOOL: 7
  BLOBMSG_CAST_INT64: 8

Fix this by changing the enum defintion to assign BLOBMSG_CAST_INT64 to
the unique value 9.

Signed-off-by: Peter Seiderer <ps.report@gmx.net>
4 years agoutils: simplify mkdir_p boolean conditions
Rui Salvaterra [Sun, 13 Dec 2020 11:57:43 +0000 (11:57 +0000)]
utils: simplify mkdir_p boolean conditions

Just a trivial simplification.

Signed-off-by: Rui Salvaterra <rsalvaterra@gmail.com>
4 years agoutils: introduce mkdir_p
Daniel Golle [Sat, 12 Dec 2020 22:45:53 +0000 (22:45 +0000)]
utils: introduce mkdir_p

Add new utility function mkdir_p(char *path, mode_t mode) to replace
the partially buggy implementations found accross fstools and procd.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
5 years agoblobmsg: introduce BLOBMSG_CAST_INT64
Daniel Golle [Tue, 4 Aug 2020 00:27:09 +0000 (01:27 +0100)]
blobmsg: introduce BLOBMSG_CAST_INT64

When dealing with 64-bit integers in JSON documents, blobmsg_parse
becomes useless as blobmsg-json only uses BLOBMSG_TYPE_INT64 if the
value exceeds the range of a 32-bit integer, otherwise
BLOBMSG_TYPE_INT32 is used. This is because blobmsg-json parses the
JSON document ad-hoc without knowing the schema in advance and hence
a result of the design of blobmsg-json (and the absence of JSON
schema definitions).
In practise, this made code less readable as instead of using
blobmsg_parse() one had to to deal with *all* attributes manually just
to catch fields which can be both, BLOBMSG_TYPE_INT32 or
BLOBMSG_TYPE_INT64, but are always dealt with as uint64_t in code as
they potentially could exceed the 32-bit range.

To resolve this issue, introduce as special wildcard attribute
type BLOBMSG_CAST_INT64 which should only be used in policies used
by blobmsg_parse(). If used for an attribute in the policy,
blobmsg_parse shall accept all integer types and allow the user
to retrieve the value using the uint64_t blobmsg_cast_u64() and
int64_t blobmsg_cast_s64() functions which is also introduced by this
commit.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
5 years agoexamples/lua: attempt to highlight some traps
Karl Palsson [Tue, 30 Jun 2020 10:38:44 +0000 (10:38 +0000)]
examples/lua: attempt to highlight some traps

Ran into some issues with my fd event being garbage collected.  As I
never wanted to call :delete, I had seen no reason to keep the returned
object, as my callback and upvalues were still valid.

Signed-off-by: Karl Palsson <karlp@etactica.com>
5 years agolua/uloop: fd_add: use absolute indices for arguments
Karl Palsson [Tue, 30 Jun 2020 10:38:43 +0000 (10:38 +0000)]
lua/uloop: fd_add: use absolute indices for arguments

Instead of having to adjust the index repeatedly as the stack is
manipulated, use absolute addressing for the function arguments, so they
stay the same throughout the call.  Zero functional change, just
subjectively easier to follow variables.

Signed-off-by: Karl Palsson <karlp@etactica.com>
5 years agolua/uloop: make get_sock_fd capable of absolute addresses
Karl Palsson [Tue, 30 Jun 2020 10:38:42 +0000 (10:38 +0000)]
lua/uloop: make get_sock_fd capable of absolute addresses

The original code required the use of relative addresses into the lua
stack.  It should accept either.

Signed-off-by: Karl Palsson <karlp@etactica.com>
5 years agolua/uloop: fd_add() better args checking
Karl Palsson [Tue, 30 Jun 2020 10:38:41 +0000 (10:38 +0000)]
lua/uloop: fd_add() better args checking

Actually check for flags being valid, instead of simply ignoring the
call if flags was zero.

Use standard lua checks for the function argument, so you can get a
normal "argument #2 was invalid, expected function, got xxx" instead of
the vague, "invalid arg list"

Signed-off-by: Karl Palsson <karlp@etactica.com>
5 years agoblobmsg: drop old comment about json formatting functions
Rafał Miłecki [Tue, 26 May 2020 08:50:58 +0000 (10:50 +0200)]
blobmsg: drop old comment about json formatting functions

Those functions were moved out of blobmsg.h.

Fixes: 0918243e90e6 ("move json formatting to the blobmsg_json library")
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
5 years agoblobmsg: fix missing length checks
Felix Fietkau [Mon, 25 May 2020 10:40:04 +0000 (12:40 +0200)]
blobmsg: fix missing length checks

blobmsg_check_attr_len was calling blobmsg_check_data for some, but not all
attribute types. These checks was missing for arrays and tables.

Additionally, the length check in blobmsg_check_data was a bit off, since
it was comparing the blobmsg data length against the raw blob attr length.

Fix this by checking the raw blob length against the buffer length in
blobmsg_hdr_from_blob

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 years agoblobmsg: simplify and fix name length checks in blobmsg_check_name
Felix Fietkau [Mon, 25 May 2020 12:49:35 +0000 (14:49 +0200)]
blobmsg: simplify and fix name length checks in blobmsg_check_name

blobmsg_hdr_valid_namelen was omitted when name==false
The blob_len vs blobmsg_namelen changes were not taking into account
potential padding between name and data

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 years agoblobmsg: fix length in blobmsg_check_array
Felix Fietkau [Mon, 25 May 2020 11:44:20 +0000 (13:44 +0200)]
blobmsg: fix length in blobmsg_check_array

blobmsg_check_array_len expects the length of the full attribute buffer,
not just the data length.
Due to other missing length checks (fixed in the next commit), this did
not show up as a test failure

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 years agotests: add fuzzer seed file for crash in blob_len
Petr Štetiar [Tue, 26 May 2020 07:22:13 +0000 (09:22 +0200)]
tests: add fuzzer seed file for crash in blob_len

Following regression was introduced in commit 5e75160f4878 ("blobmsg:
fix attrs iteration in the blobmsg_check_array_len()"):

 Thread 1 "test-fuzz" received signal SIGSEGV, Segmentation fault.
  in blob_len (attr=0x6020000100d4) at libubox/blob.h:102
  102             return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);

 blob_len (attr=0x6020000100d4) at /libubox/blob.h:102
 blob_raw_len (attr=0x6020000100d4) at /libubox/blob.h:111
 blob_pad_len (attr=0x6020000100d4) at /libubox/blob.h:120
 blobmsg_check_array_len (attr=0x6020000000d0, type=0, blob_len=10) at /libubox/blobmsg.c:145
 fuzz_blobmsg_parse (data=0x6020000000d0 "\001\004", size=10) at /libubox/tests/fuzz/test-fuzz.c:57

Signed-off-by: Petr Štetiar <ynezz@true.cz>
5 years agoblob: make blob_parse_untrusted more permissive
Matthias Schiffer [Sat, 16 May 2020 20:22:10 +0000 (22:22 +0200)]
blob: make blob_parse_untrusted more permissive

Some tools like ucert use concatenations of multiple blobs. Account for
this case by allowing the underlying buffer length to be greater than
the blob length.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
5 years agoblobmsg: fix attrs iteration in the blobmsg_check_array_len()
Rafał Miłecki [Sat, 23 May 2020 11:18:51 +0000 (13:18 +0200)]
blobmsg: fix attrs iteration in the blobmsg_check_array_len()

Starting with 75e300aeec25 ("blobmsg: fix wrong payload len passed from
blobmsg_check_array") blobmsg_check_array_len() gets *blob* length
passed as argument. It cannot be used with __blobmsg_for_each_attr()
which expects *data* length.

Use blobmsg_for_each_attr() which calculates *data* length on its own.

The same bug was already reported in the past and there was fix attempt
in the commit cd75136b1342 ("blobmsg: fix wrong payload len passed from
blobmsg_check_array"). That change made blobmsg_check_attr_len() calls
fail however.

This is hopefully the correct & complete fix:
1. blobmsg_check_array_len() gets *blob* length
2. It calls blobmsg_check_attr_len() which requires *blob* length
3. It uses blobmsg_for_each_attr() which gets *data* length

This fixes iterating over random memory treated as attrs. That was
resulting in check failing randomly for totally correct blobs. It's
critical e.g. for procd project with its instance_fill_array() failing
and procd not starting services.

Fixes: 75e300aeec25 ("blobmsg: fix wrong payload len passed from blobmsg_check_array")
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
5 years agotests: runqueue: try to fix race on GitLab CI
Petr Štetiar [Thu, 21 May 2020 14:28:29 +0000 (16:28 +0200)]
tests: runqueue: try to fix race on GitLab CI

Seems like the CI runners are slower and produce different test output:

 -  [0/1] finish 'sleep 1' (killer)
    [1/1] start 'sleep 1' (sleeper)
 +  [1/1] finish 'sleep 1' (killer)
 +  [1/1] finish 'sleep 1' (killer)
    [1/1] cancel 'sleep 1' (sleeper)
    [0/1] finish 'sleep 1' (sleeper)
    [1/1] start 'sleep 1' (sleeper)

Lets try to fix it by lowering the killing timeout.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
5 years agolibubox: runqueue: fix use-after-free bug
Alban Bedel [Thu, 23 Apr 2020 03:35:23 +0000 (15:35 +1200)]
libubox: runqueue: fix use-after-free bug

Fixes a use-after-free bug in runqueue_task_kill():

 Invalid read of size 8
    at runqueue_task_kill (runqueue.c:200)
    by uloop_process_timeouts (uloop.c:505)
    by uloop_run_timeout (uloop.c:542)
    by uloop_run (uloop.h:111)
    by main (tests/test-runqueue.c:126)
  Address 0x5a4b058 is 24 bytes inside a block of size 208 free'd
    at free
    by runqueue_task_complete (runqueue.c:234)
    by runqueue_task_kill (runqueue.c:199)
    by uloop_process_timeouts (uloop.c:505)
    by uloop_run_timeout (uloop.c:542)
    by uloop_run (uloop.h:111)
    by main (tests/test-runqueue.c:126)
  Block was alloc'd at
    at calloc
    by add_sleeper (tests/test-runqueue.c:101)
    by main (tests/test-runqueue.c:123)

Since commit 11e8afea (runqueue should call the complete handler from
more places) the call to the complete() callback has been moved to
runqueue_task_complete().  However in runqueue_task_kill()
runqueue_task_complete() is called before the kill() callback.  This
will result in a use after free if the complete() callback frees the
task struct.

Furthermore runqueue_start_next() is already called at the end of
runqueue_task_complete(), so there is no need to call it again in
runqueue_task_kill().

The issue was that the _complete() callback frees the memory used by the
task struct, which is then read after the _complete() callback returns.

Ref: FS#3016
Signed-off-by: Alban Bedel <albeu@free.fr>
[initial test case, kill cb comment fix]
Signed-off-by: Chris Nisbet <nischris@gmail.com>
[testcase improvements and commit subject/description tweaks]
Signed-off-by: Petr Štetiar <ynezz@true.cz>
5 years agolibubox: runqueue fix comment in header
Chris Nisbet [Thu, 23 Apr 2020 03:35:24 +0000 (15:35 +1200)]
libubox: runqueue fix comment in header

The comment relating to the runqueue task structure 'cancel' callback
indicated that the callback 'calls' runqueue_task_complete, which
isn't quite right. The callback _should_ call runqueue_task_complete.

Signed-off-by: Chris Nisbet <nischris@gmail.com>
5 years agotests: list: add test case for list_empty iterator
Petr Štetiar [Thu, 19 Dec 2019 10:49:39 +0000 (11:49 +0100)]
tests: list: add test case for list_empty iterator

Increasing unit testing code coverage.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
5 years agotests: blobmsg: add test case
Chris Nisbet [Wed, 12 Feb 2020 08:08:44 +0000 (21:08 +1300)]
tests: blobmsg: add test case

 * add a test for blobmsg_check_array() to test an array with a string in it

This test was added in conjunction with a change to blobmsg_check_array() to
get it to pass the length obtained from blob_len() rather than blobmsg_len().

Signed-off-by: Chris Nisbet <nischris@gmail.com>
5 years agoblobmsg: fix wrong payload len passed from blobmsg_check_array
Chris Nisbet [Wed, 12 Feb 2020 08:00:31 +0000 (21:00 +1300)]
blobmsg: fix wrong payload len passed from blobmsg_check_array

Fix incorrect use of blobmsg_len() on passed blobmsg to
blobmsg_check_array_len() introduced in commit 379cd33d1992
("fix wrong payload len passed from blobmsg_check_array") by using correct
blob_len().

By using blobmsg_len() a value too small was passed to blobmsg_check_array()
which could lead to this function returning an error when there is none.

Fixes: 379cd33d1992 ("fix wrong payload len passed from blobmsg_check_array")
Signed-off-by: Chris Nisbet <nischris@gmail.com>
[add fixes tag, rewrap commit message]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
5 years agoblobmsg: blobmsg_parse and blobmsg_parse_array oob read fixes
Juraj Vijtiuk [Sun, 12 Jan 2020 11:26:18 +0000 (12:26 +0100)]
blobmsg: blobmsg_parse and blobmsg_parse_array oob read fixes

Fix out of bounds read in blobmsg_parse and blobmsg_check_name. The
out of bounds read happens because blob_attr and blobmsg_hdr have
flexible array members, whose size is 0 in the corresponding sizeofs.
For example the __blob_for_each_attr macro checks whether rem >=
sizeof(struct blob_attr). However, what LibFuzzer discovered was,
if the input data was only 4 bytes, the data would be casted to blob_attr,
and later on blob_data(attr) would be called even though attr->data was empty.
The same issue could appear with data larger than 4 bytes, where data
wasn't empty, but contained only the start of the blobmsg_hdr struct,
and blobmsg_hdr name was empty. The bugs were discovered by fuzzing
blobmsg_parse and blobmsg_array_parse with LibFuzzer.

CC: Luka Perkov <luka.perkov@sartura.hr>
Reviewed-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Juraj Vijtiuk <juraj.vijtiuk@sartura.hr>
[refactored some checks, added fuzz inputs, adjusted unit test results]
Signed-off-by: Petr Štetiar <ynezz@true.cz>
5 years agotests: prefer dynamically allocated buffers
Petr Štetiar [Sat, 18 Jan 2020 17:32:55 +0000 (18:32 +0100)]
tests: prefer dynamically allocated buffers

Help detecting Valgrind OOB reads and other issues.

 Conditional jump or move depends on uninitialised value(s)
   at 0x5452886: blobmsg_parse (blobmsg.c:203)
   by 0x400A8E: test_blobmsg (tests/test-blobmsg-parse.c:66)
   by 0x400A8E: main (tests/test-blobmsg-parse.c:82)

 Conditional jump or move depends on uninitialised value(s)
   at 0x545247F: blobmsg_check_name (blobmsg.c:39)
   by 0x545247F: blobmsg_check_attr_len (blobmsg.c:79)
   by 0x5452710: blobmsg_parse_array (blobmsg.c:159)
   by 0x400AB8: test_blobmsg (tests/test-blobmsg-parse.c:69)
   by 0x400AB8: main (tests/test-blobmsg-parse.c:82)

 Conditional jump or move depends on uninitialised value(s)
   at 0x54524A0: blobmsg_check_name (blobmsg.c:42)
   by 0x54524A0: blobmsg_check_attr_len (blobmsg.c:79)
   by 0x5452710: blobmsg_parse_array (blobmsg.c:159)
   by 0x400AB8: test_blobmsg (tests/test-blobmsg-parse.c:69)
   by 0x400AB8: main (tests/test-blobmsg-parse.c:82)

Ref: http://lists.infradead.org/pipermail/openwrt-devel/2020-January/021204.html
Signed-off-by: Petr Štetiar <ynezz@true.cz>
5 years agoblobmsg_json: prefer snprintf usage
Petr Štetiar [Tue, 14 Jan 2020 08:05:02 +0000 (09:05 +0100)]
blobmsg_json: prefer snprintf usage

Better safe than sorry and while at it prefer use of PRId16 and PRId32
formatting constants as well.

Reviewed-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Petr Štetiar <ynezz@true.cz>
5 years agoblobmsg: blobmsg_vprintf: prefer vsnprintf
Petr Štetiar [Tue, 14 Jan 2020 07:57:05 +0000 (08:57 +0100)]
blobmsg: blobmsg_vprintf: prefer vsnprintf

Better safe than sorry and while at it add handling of possible
*printf() failures.

Reviewed-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Petr Štetiar <ynezz@true.cz>
5 years agojshn: prefer snprintf usage
Petr Štetiar [Tue, 14 Jan 2020 07:55:34 +0000 (08:55 +0100)]
jshn: prefer snprintf usage

Better safe than sorry.

Reviewed-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Petr Štetiar <ynezz@true.cz>