aboutsummaryrefslogtreecommitdiffstats
path: root/lib/path.c
AgeCommit message (Collapse)AuthorFilesLines
2025-07-02lib/path: avoid double free() for cpusetsKarel Zak1-1/+3
Addresses: https://github.com/util-linux/util-linux/issues/3641 Signed-off-by: Karel Zak <kzak@redhat.com>
2025-06-06remove "Copyright (C) ...." notes from files that claim no copyrightBenno Schulenberg1-1/+1
Replace them with a "Written by" or "Authors:" label. Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2024-05-13lib/path: use _vreadf_buffer for _cpuparse()Thomas Weißschuh1-20/+2
ul_path_vreadf_buffer already provides zero-termination and newline trimming, so make use of it. We now could read more than a single line, but the files this is used on contain only a single line anyways. Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
2024-05-13lib/path: introduce ul_path_vreadf_bufferThomas Weißschuh1-3/+12
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
2024-05-13lib/path: use _read_buffer for _read_string()Thomas Weißschuh1-8/+2
ul_path_read_buffer already provides zero-termination and newline trimming, so make use of it. Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
2024-05-13Merge branch 'fix' of https://github.com/DaanDeMeyer/util-linuxKarel Zak1-1/+1
* 'fix' of https://github.com/DaanDeMeyer/util-linux: Fix ul_path_read_buffer()
2024-05-09Fix ul_path_read_buffer()Daan De Meyer1-1/+1
The current implementation cuts off the last character of the buffer if there is no trailing newline.
2024-05-03lib/path: add ul_path_statf() and ul_path_vstatf()Karel Zak1-0/+19
Signed-off-by: Karel Zak <kzak@redhat.com> (cherry picked from commit 9b47899495e229ace482afca9e14b9b95ef6fde9)
2023-10-10lib/path: Set errno in case of fgets failureTobias Stoeckmann1-1/+6
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2023-10-08lib/path: fix typosTobias Stoeckmann1-6/+6
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2023-10-08lib/path: remove ul_prefix_fopenTobias Stoeckmann1-19/+0
The function ul_prefix_fopen does not properly check for paths which are too long. Since the only caller of ul_prefix_fopen can be easily converted to ul_path_fopen, remove the function. Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2023-10-08lib/path: set errno in case of errorTobias Stoeckmann1-2/+4
Some programs like lscpu expect errno to be set if ul_path_cpuparse fails. Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2023-10-08lib/path: fix possible out of boundary accessTobias Stoeckmann1-1/+1
If fgets reads from a file starting with a NUL byte in ul_path_cpuparse, then the check for newline leads to an out of boundary access. Proof of Concept (compile with --enable-asan): 1. Prepare /tmp/poc with required files ``` $ install -d /tmp/poc/sys/devices/system/cpu $ dd if=/dev/zero of=/tmp/poc/sys/devices/system/cpu/possible bs=1 count=1 $ install -D /dev/null /tmp/poc/proc/cpuinfo ``` 2. Run lscpu with sysroot option ``` $ lscpu --sysroot /tmp/poc ================================================================= ==78238==ERROR: AddressSanitizer: heap-buffer-overflow ``` Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2023-09-13lib/path: remove usage of VLAThomas Weißschuh1-11/+25
Variable-length-arrays are susceptible to security issues, avoid them. Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
2023-06-20lib/ include/: cleanup licence headersKarel Zak1-5/+6
This patch does not change any license of the affected files. Changes: * add missing SPDX-License-Identifier lines for LGPL * copy missing license lines from code (lib/) to header files (include/) * use the same comment formatting Signed-off-by: Karel Zak <kzak@redhat.com>
2022-09-27lib/path: ul_path_cpuparse: fix parsing of empty sysfs filesPetr Štetiar1-1/+1
Kernel 5.15 returns empty content for topology/thread_siblings on aarch64 platform, which in conjunction of uninitialized `buf` memory buffer results in the garbage: (gdb) p buf $14 = " @\377\367\177\000\000\000\275\000\347j\032\236" This garbage is then being later consumed by underlying helper functions like for example cpumask_parse() and this leads to the following crash later: in __libc_free (p=0x7ff7f67c00) at src/malloc/mallocng/free.c:105 in free (p=<optimized out>) at src/malloc/free.c:5 in add_cpuset_to_array (setsize=<optimized out>, set=<optimized out>, items=<optimized out>, ary=<optimized out>) at ../sys-utils/lscpu-topology.c:29 in cputype_read_topology (cxt=cxt@entry=0x7ff7fffe70, ct=0x4298a0) at ../sys-utils/lscpu-topology.c:153 in lscpu_read_topology (cxt=cxt@entry=0x7ff7fffe70) at ../sys-utils/lscpu-topology.c:629 in main (argc=1, argv=0x7ffffffdb8) at ../sys-utils/lscpu.c:1341 It looks like the problem is that current logic expects fgets() to set errno on failure, but fgets() is not documented to do so and and neither glibc nor musl set errno. So if errno was set to 0 before fgets() call, the failure from fgets() is ignored and then invalid buffer is being parsed. Fixes: #1810 Suggested-by: Thomas Weißschuh <thomas@t-8ch.de> Signed-off-by: Petr Štetiar <ynezz@true.cz>
2022-03-17lib/path: initialize variables for scanf [coverity scan]Karel Zak1-2/+2
Signed-off-by: Karel Zak <kzak@redhat.com>
2022-03-17lib/path: make ul_path_read_buffer() more robust [coverity scan]Karel Zak1-7/+10
Make sure we never call buf[rc - 1] for rc=0. Signed-off-by: Karel Zak <kzak@redhat.com>
2021-11-29lib/path: make path use more robust [coverity scan]Karel Zak1-1/+4
*** CID 374365: Null pointer dereferences (FORWARD_NULL) /lib/path.c: 364 in ul_path_stat() Signed-off-by: Karel Zak <kzak@redhat.com>
2021-10-06lib/path: add ul_path_next_dirent()Karel Zak1-0/+21
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-10-06lib/path: use flags for fstatat()Karel Zak1-2/+3
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-10-06lib/path: fstat dir itselfKarel Zak1-4/+6
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-18lib/path: (test) fix ul_new_path() useKarel Zak1-1/+1
2021-06-17lib/path: improve ul_path_readlink() to be more robustKarel Zak1-8/+13
According to POSIX, readlink() makes no effort to null-terminate buffer with the result. It seems better to hide this disadvantage in the ul_path_...() API rather than assume buf[sz] = '\0' everywhere. Reported-by: Reported-by: Jan Pazdziora <jpazdziora@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-11lib/path: fix possible leak when use ul_path_read_string() [coverity scan]Karel Zak1-3/+5
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-07-08lib/path: add ul_path_is_accessible()Karel Zak1-0/+14
This function allow to check that path_cxt is usable. Note that ul_new_path() does not open the path. Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-15lib/path: add ul_path_stat(), fix absolute pathsKarel Zak1-11/+51
* add ul_path_stat() * make sure all paths for ul_path_..() functions are always interpreted relatively to the context directory and prefix. This is difference between ul_path_ API and standard libc "at" functions. We do not use any exception for absolute paths. The reason is that we need to read from prefixed paths although application assume absolute path (/dev/sda1 means /prefix/dev/sda1 if a /prefix is defined). Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-17lib/path: make sure ul_path_read_buffer() derminate resultKarel Zak1-2/+3
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-20lib/path: use xstrncpy()Karel Zak1-2/+2
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15lib/path: fix resource leak [coverity scan]Karel Zak1-2/+5
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15lib/path: fix possible NULL dereferencing [coverity scan]Karel Zak1-2/+4
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15lib/path: fix ul_path_get_dirfd() usage [coverity scan]Karel Zak1-1/+5
ul_path_get_dirfd() returns negative number on error... Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15lib/path: consolidate ul_path_mkpath() usageKarel Zak1-74/+28
* always use errno from ul_path_mkpath() * keep code simple and short as possible Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15lib/path: fix possible NULL pointer dereferencing [coverity scan]Karel Zak1-0/+3
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-11lib/fileutils: add xreaddir()Karel Zak1-15/+0
Remove duplicate code and keep only one implementation in include/fileutils.h. Signed-off-by: Karel Zak <kzak@redhat.com>
2019-03-19lib/path: remove extra semi-colonsKarel Zak1-3/+3
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-02-18docs: fix typos [codespell]Sami Kerola1-2/+2
Reference: https://github.com/codespell-project/codespell Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2018-12-10choom: fix negative adjust score usageKarel Zak1-0/+22
It's really bad idea to use uint64_t (ul_path_write_u64(()) when write signed number. Addresses: https://github.com/karelzak/util-linux/issues/723 Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-07lib/path: allow to close dirfdKarel Zak1-3/+19
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-25lib/path: remove unused expressionRuediger Meier1-1/+1
error: expression result unused [-Werror,-Wunused-value] prefix = optarg, "failed to parse range start"; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2018-06-21lib/path: allow dir-path formattingKarel Zak1-3/+9
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lsblk: fix ul_path_read_string() usageKarel Zak1-4/+4
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: improve debug message on redirectKarel Zak1-3/+3
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: simplify debug messageKarel Zak1-2/+2
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: add ul_prefix_fopen(), improve cpuset funcsKarel Zak1-2/+27
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: don't use extra '/'Karel Zak1-4/+16
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: add more debug messagesKarel Zak1-2/+6
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: make ul_path_read_ usable with NULL handlerKarel Zak1-11/+22
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: add ul_path_read_buffer()Karel Zak1-0/+29
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: add ul_path_get_abspath()Karel Zak1-1/+33
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/sysfs: fix ul_path_read_string() usageKarel Zak1-0/+1
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/sysfs: make possible to call sysfs_blkdev_deinit_path() in loopKarel Zak1-1/+1
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: fix read string to be backwardly compatibleKarel Zak1-0/+4
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path lib/sysfs: add debugKarel Zak1-1/+37
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21lib/path: new implementationKarel Zak1-139/+907
The goal is to avoid duplicate code in path.c and sysfs.c and make it possible to define prefix for paths for all sysfs and procfs based utils. Now we have /proc snapshots (for tests) for lscpu only. It would be nice to have the same (for sysfs) for lsblk and another tools. * very simple API to read numbers, strings and symlinks * based on openat() pc = ul_new_path("/sys/block/sda"); ul_path_read_u64(pc, &size, "size"); ul_path_read_u64(pc, &lsz, "queue/logical_block_size"); * printf-like API to generate paths, for example: ul_path_readf_u64(pc, &num, "sda%d/size", partno) * allow to define prefix to redirect hardcoded paths to another location, for example: pc = ul_new_path("/sys/block/sda"); ul_path_set_prefix(pc, "/my/regression/dump"); ul_path_read_u64(pc, &num, "size"); to read /my/regression/dump/sys/block/sda/size * allow to extend the API by "dialects", for example for sysfs: pc = ul_new_path(NULL); sysfs_blkdev_init_path(pc, devno, NULL); and use ul_path_* functions to read from @pc initialized by sysfs_blkdev_init_path() * add test_path binary Signed-off-by: Karel Zak <kzak@redhat.com>
2017-10-23lib/path: make path_set_prefix() independent on cpu_set_tKarel Zak1-15/+14
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-06-29lsmem: fix, using freed memoryRuediger Meier1-3/+3
Simply avoiding strdup(). Error handling improved. This was the Clang Analyzer warning: Memory Error, Use-after-free sys-utils/lsmem.c:259:3: warning: Use of memory after it is freed err(EXIT_FAILURE, _("Failed to open %s"), path); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29lib/path: add error handling to path_vcreate()Ruediger Meier1-9/+25
Do not operate on truncated/random paths. Note, path_strdup() can now really return NULL, to be handled in next commit. Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-27lib/path: fix crash, pathbuf overflowRuediger Meier1-4/+10
Before: $ lscpu -s "$(tr '\0' 'x' < /dev/zero | head -c 10000)" Segmentation fault (core dumped) After: $ lscpu -s "$(tr '\0' 'x' < /dev/zero | head -c 10000)" lscpu: invalid argument to --sysroot: File name too long Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-11lib/path: ifdef path_set_prefix() like in path.hRuediger Meier1-2/+2
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2015-01-26docs: fix two "maybe be" duplications in program commentsBenno Schulenberg1-5/+2
Also improve the grammar of the comments and some neighbours. Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2014-01-06lib/path: add path_strdup()Karel Zak1-0/+13
Signed-off-by: Karel Zak <kzak@redhat.com>
2013-10-09textual: standardize some "cannot read" and "seek failed" error messagesBenno Schulenberg1-4/+4
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2013-04-03lib: use O_CLOEXEC in libcommonKarel Zak1-4/+4
Signed-off-by: Karel Zak <kzak@redhat.com>
2012-11-23lib/path: add path_read_u64()Karel Zak1-0/+22
Signed-off-by: Karel Zak <kzak@redhat.com>
2012-11-23lib/path: rename functions to be more explicitKarel Zak1-8/+10
... and to have names compatible with lib/sysfs.c Signed-off-by: Karel Zak <kzak@redhat.com>
2012-11-02build-sys: cleanup cpu_set_t usageKarel Zak1-1/+4
- make taskset depend on cpu_set_t - make cpuset.c optional for libcommon and lib/path.c Signed-off-by: Karel Zak <kzak@redhat.com>
2012-07-16translation: unify file open error messagesSami Kerola1-2/+2
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2012-05-15include: rename writeall.h to all-io.hPetr Uzel1-1/+1
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
2012-02-24docs: corrections to FSF license files, and postal addressSami Kerola1-3/+3
The COPYING and Documentation/licenses/COPYING* files are being replaced by files from GNU web site. http://www.gnu.org/licenses/gpl-2.0.txt http://www.gnu.org/licenses/lgpl-2.1.txt Postal addresses to FSF in other files are updated to match with the address in license files. Reference: http://lists.gnu.org/archive/html/freefont-announce/2005-04/msg00001.html Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2011-09-27lib,path: use write_all()Heiko Carstens1-1/+2
From: Heiko Carstens <heiko.carstens@de.ibm.com> Since write() doesn't necessarily write the complete buffer with one call we better use write_all() which takes care of this. Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
2011-09-27chcpu: convert to use lib/path.cHeiko Carstens1-0/+26
Use the common path access functions. In order to simplify chcpu also implement and use path_writestr() which writes a string to the path specified. Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
2011-09-27lib,path: move path access functions from lscpu into lib/path.cHeiko Carstens1-0/+191
A couple of these functions already have been copied to chcpu.c, so it makes sense to move these functions into an own file. Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>