From d260d918dd3f02370bb60de29de9a299e14f58d1 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 29 Jul 2024 17:05:46 -0400 Subject: [PATCH 1/2] tests: Add a payload link unit test Motivated by changing this code. Signed-off-by: Colin Walters --- Makefile-tests.am | 1 + tests/test-payload-link.sh | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100755 tests/test-payload-link.sh diff --git a/Makefile-tests.am b/Makefile-tests.am index d333b9a3c1..abf61af2d0 100644 --- a/Makefile-tests.am +++ b/Makefile-tests.am @@ -73,6 +73,7 @@ _installed_or_uninstalled_test_scripts = \ tests/test-remote-headers.sh \ tests/test-remote-refs.sh \ tests/test-composefs.sh \ + tests/test-payload-link.sh \ tests/test-commit-sign.sh \ tests/test-commit-timestamp.sh \ tests/test-export.sh \ diff --git a/tests/test-payload-link.sh b/tests/test-payload-link.sh new file mode 100755 index 0000000000..14b86a6245 --- /dev/null +++ b/tests/test-payload-link.sh @@ -0,0 +1,46 @@ +#!/bin/bash +# +# Copyright (C) 2024 Red Hat, Inc. +# +# SPDX-License-Identifier: LGPL-2.0+ +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see . + +set -euox pipefail + +. $(dirname $0)/libtest.sh + +touch foo +if ! cp --reflink=always foo bar &>/dev/null; then + skip "no reflinks" +fi +rm foo bar + +$CMD_PREFIX ostree --repo=repo init --mode=bare-user +$CMD_PREFIX ostree config --repo=repo set core.payload-link-threshold 0 + +mkdir d +echo test > d/testcontent +chmod 0644 d/testcontent +$CMD_PREFIX ostree --repo=repo commit --tree=dir=d --consume -b testref +mkdir d +echo test > d/testcontent +chmod 0755 d/testcontent +$CMD_PREFIX ostree --repo=repo commit --tree=dir=d --consume -b testref +find repo -type l -name '*.payload-link' >payload-links.txt +assert_streq "$(wc -l < payload-links.txt)" "1" + +tap_ok payload-link + +tap_end From f08cea998e3d0a1e84eea0a842a4cee26704f852 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 29 Jul 2024 15:17:10 -0400 Subject: [PATCH 2/2] repo: NUL terminate readlinkat result Coverity was correctly complaining about this. Signed-off-by: Colin Walters --- src/libostree/ostree-repo-commit.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libostree/ostree-repo-commit.c b/src/libostree/ostree-repo-commit.c index 4d12d5ecb4..db83ebf240 100644 --- a/src/libostree/ostree-repo-commit.c +++ b/src/libostree/ostree-repo-commit.c @@ -794,7 +794,7 @@ _try_clone_from_payload_link (OstreeRepo *self, OstreeRepo *dest_repo, const cha glnx_autofd int fdf = -1; char loose_path_buf[_OSTREE_LOOSE_PATH_MAX]; char loose_path_target_buf[_OSTREE_LOOSE_PATH_MAX]; - char target_buf[_OSTREE_LOOSE_PATH_MAX + _OSTREE_PAYLOAD_LINK_PREFIX_LEN]; + char target_buf[_OSTREE_LOOSE_PATH_MAX + _OSTREE_PAYLOAD_LINK_PREFIX_LEN + 1]; char target_checksum[OSTREE_SHA256_STRING_LEN + 1]; int dfd = dfd_searches[i]; ssize_t size; @@ -804,16 +804,21 @@ _try_clone_from_payload_link (OstreeRepo *self, OstreeRepo *dest_repo, const cha _ostree_loose_path (loose_path_buf, payload_checksum, OSTREE_OBJECT_TYPE_PAYLOAD_LINK, self->mode); - size = TEMP_FAILURE_RETRY (readlinkat (dfd, loose_path_buf, target_buf, sizeof (target_buf))); + size = TEMP_FAILURE_RETRY ( + readlinkat (dfd, loose_path_buf, target_buf, sizeof (target_buf) - 1)); if (size < 0) { if (errno == ENOENT) continue; return glnx_throw_errno_prefix (error, "readlinkat"); } + target_buf[size] = '\0'; + const size_t expected_len = OSTREE_SHA256_STRING_LEN + _OSTREE_PAYLOAD_LINK_PREFIX_LEN; if (size < OSTREE_SHA256_STRING_LEN + _OSTREE_PAYLOAD_LINK_PREFIX_LEN) - return glnx_throw (error, "invalid data size for %s", loose_path_buf); + return glnx_throw (error, "invalid data size for %s; expected=%llu found=%llu", + loose_path_buf, (unsigned long long)expected_len, + (unsigned long long)size); snprintf (target_checksum, size, "%.2s%.62s", target_buf + _OSTREE_PAYLOAD_LINK_PREFIX_LEN, target_buf + _OSTREE_PAYLOAD_LINK_PREFIX_LEN + 3);