| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/cvmfs_suid_util.cc |
| Date: | 2025-11-09 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 33 | 34 | 97.1% |
| Branches: | 39 | 60 | 65.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "cvmfs_suid_util.h" | ||
| 6 | |||
| 7 | #include <sys/stat.h> | ||
| 8 | #include <unistd.h> | ||
| 9 | |||
| 10 | #include <cassert> | ||
| 11 | #include <climits> | ||
| 12 | #include <cstdlib> | ||
| 13 | |||
| 14 | #include "sanitizer.h" | ||
| 15 | |||
| 16 | using namespace std; // NOLINT | ||
| 17 | |||
| 18 | namespace cvmfs_suid { | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Makes a systemd mount unit string from the given path, such as | ||
| 22 | * a-b-c.mount from /a/b/c | ||
| 23 | */ | ||
| 24 | 368 | string EscapeSystemdUnit(const string &path) { | |
| 25 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 368 times.
|
368 | assert(!path.empty()); |
| 26 | |||
| 27 |
1/2✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
|
368 | string normalized_path(path); |
| 28 | size_t pos; | ||
| 29 |
2/2✓ Branch 1 taken 322 times.
✓ Branch 2 taken 368 times.
|
690 | while ((pos = normalized_path.find("//")) != string::npos) { |
| 30 |
1/2✓ Branch 1 taken 322 times.
✗ Branch 2 not taken.
|
322 | normalized_path.replace(pos, 2, "/"); |
| 31 | } | ||
| 32 | |||
| 33 |
2/2✓ Branch 1 taken 184 times.
✓ Branch 2 taken 184 times.
|
368 | if (normalized_path == "/") |
| 34 |
1/2✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
|
184 | return "-.mount"; |
| 35 | |||
| 36 |
2/4✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 184 times.
✗ Branch 6 not taken.
|
368 | const sanitizer::InputSanitizer sanitizer("az AZ 09 _"); |
| 37 | 184 | const unsigned length = normalized_path.length(); | |
| 38 | 184 | string result; | |
| 39 |
2/2✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 184 times.
|
1472 | for (unsigned i = 0; i < length; ++i) { |
| 40 |
1/2✓ Branch 1 taken 1288 times.
✗ Branch 2 not taken.
|
1288 | const char c = normalized_path[i]; |
| 41 |
2/2✓ Branch 0 taken 552 times.
✓ Branch 1 taken 736 times.
|
1288 | if (c == '/') { |
| 42 |
4/4✓ Branch 0 taken 414 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 368 times.
|
552 | if ((i == 0) || (i == length - 1)) |
| 43 | 184 | continue; | |
| 44 |
1/2✓ Branch 1 taken 368 times.
✗ Branch 2 not taken.
|
368 | result.push_back('-'); |
| 45 |
4/4✓ Branch 0 taken 92 times.
✓ Branch 1 taken 644 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 46 times.
|
736 | } else if ((c == '.') && (i > 0)) { |
| 46 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | result.push_back('.'); |
| 47 |
4/6✓ Branch 2 taken 690 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 690 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 598 times.
✓ Branch 10 taken 92 times.
|
690 | } else if (sanitizer.IsValid(string(&c, 1))) { |
| 48 |
1/2✓ Branch 1 taken 598 times.
✗ Branch 2 not taken.
|
598 | result.push_back(c); |
| 49 | } else { | ||
| 50 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | result.push_back('\\'); |
| 51 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | result.push_back('x'); |
| 52 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | result.push_back((c / 16) + ((c / 16 <= 9) ? '0' : 'a' - 10)); |
| 53 |
3/4✓ Branch 0 taken 46 times.
✓ Branch 1 taken 46 times.
✓ Branch 3 taken 92 times.
✗ Branch 4 not taken.
|
92 | result.push_back((c % 16) + ((c % 16 <= 9) ? '0' : 'a' - 10)); |
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 |
1/2✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
|
184 | return result + ".mount"; |
| 58 | 368 | } | |
| 59 | |||
| 60 | |||
| 61 | 94 | bool PathExists(const std::string &path) { | |
| 62 | struct stat info; | ||
| 63 | 94 | const int retval = stat(path.c_str(), &info); | |
| 64 | 94 | return retval == 0; | |
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | 141 | string ResolvePath(const std::string &path) { | |
| 69 | char buf[PATH_MAX]; | ||
| 70 | 141 | char *retval = realpath(path.c_str(), buf); | |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 141 times.
|
141 | if (retval == NULL) |
| 72 | ✗ | return ""; | |
| 73 |
1/2✓ Branch 2 taken 141 times.
✗ Branch 3 not taken.
|
141 | return string(buf); |
| 74 | } | ||
| 75 | |||
| 76 | } // namespace cvmfs_suid | ||
| 77 |