Fix zero-length memcpy from NULL found by UBSan #173

Merged
vitalif merged 1 commits from koolkhel/vitastor:ubsan-fixes into master 2026-11-25 20:53:12 +03:00
Contributor

Description

memcpy() with a NULL pointer is undefined even when the length is 0, and UBSan reports it as "null pointer passed as argument 2". Skip the copy when there is nothing to copy:

  • Store v2 flusher (blockstore_flush.cpp): a zero-length small write has no checksums, so get_checksums() returns NULL when compaction merges them.
  • Store v2 list_objects() (blockstore_heap.cpp): the unstable or the tombstone list is NULL when only the other one is non-empty.
  • OSD sync with immediate_commit != all (osd_primary_sync.cpp): a dirty PG without copies to delete after sync has an empty vector whose data() is NULL, while another PG has some.
  • Disk mock (ringloop_mock.cpp): an empty iovec of a write may have a NULL iov_base.

Tested: built with -DWITH_UBSAN=yes (GCC 13) and run with UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1. On master test_pg_chaos --seed 18 reports the flusher, list_objects and disk mock cases (with halt_on_error=0; with =1 it fails on the first), and test_blockstore and test_heap fail on the list_objects one. With the fix test_pg_chaos (100 seeds), test_blockstore, test_heap, test_blockstore_v1, test_blockstore_chaos, osd_test and osd_rmw_test pass with no reports. No existing test reaches the sync case: a scratch osd_test scenario (two dirty PGs, one with a misplaced object, immediate_commit=none, then SYNC) reports osd_primary_sync.cpp:116 on master and passes with the fix.

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description memcpy() with a NULL pointer is undefined even when the length is 0, and UBSan reports it as "null pointer passed as argument 2". Skip the copy when there is nothing to copy: - Store v2 flusher (`blockstore_flush.cpp`): a zero-length small write has no checksums, so `get_checksums()` returns NULL when compaction merges them. - Store v2 `list_objects()` (`blockstore_heap.cpp`): the unstable or the tombstone list is NULL when only the other one is non-empty. - OSD sync with `immediate_commit != all` (`osd_primary_sync.cpp`): a dirty PG without copies to delete after sync has an empty vector whose `data()` is NULL, while another PG has some. - Disk mock (`ringloop_mock.cpp`): an empty iovec of a write may have a NULL `iov_base`. Tested: built with `-DWITH_UBSAN=yes` (GCC 13) and run with `UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1`. On master `test_pg_chaos --seed 18` reports the flusher, list_objects and disk mock cases (with `halt_on_error=0`; with `=1` it fails on the first), and `test_blockstore` and `test_heap` fail on the list_objects one. With the fix `test_pg_chaos` (100 seeds), `test_blockstore`, `test_heap`, `test_blockstore_v1`, `test_blockstore_chaos`, `osd_test` and `osd_rmw_test` pass with no reports. No existing test reaches the sync case: a scratch `osd_test` scenario (two dirty PGs, one with a misplaced object, `immediate_commit=none`, then SYNC) reports `osd_primary_sync.cpp:116` on master and passes with the fix. ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-11-24 11:52:38 +03:00
memcpy() with a NULL pointer is undefined even when the length is 0, and
UBSan reports it as "null pointer passed as argument 2". Skip the copy
when there is nothing to copy:

- Store v2 flusher: a zero-length small write has no checksums, so
  get_checksums() returns NULL when compaction merges them
- Store v2 list_objects(): the unstable or the tombstone list is NULL
  when only the other one is non-empty
- OSD sync with immediate_commit != all: a dirty PG without copies to
  delete after sync has an empty vector whose data() is NULL, while
  another PG has some
- disk mock: an empty iovec of a write may have a NULL iov_base

Found by test_pg_chaos --seed 18, test_blockstore and test_heap built
with WITH_UBSAN=yes.
vitalif merged commit 884727af62 into master 2026-11-25 20:53:12 +03:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#173
No description provided.