Skip to content

Commit d61d615

Browse files
committed
bin/xbps-pkgdb: check file and symlink owners & perms
1 parent a8563d8 commit d61d615

2 files changed

Lines changed: 73 additions & 8 deletions

File tree

bin/xbps-pkgdb/check_pkg_files.c

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ check_pkg_files(struct xbps_handle *xhp, const char *pkgname, void *arg)
5757
xbps_dictionary_t pkg_filesd = arg;
5858
const char *file = NULL, *sha256 = NULL;
5959
char *path;
60-
bool mutable, test_broken = false;
60+
bool mutable, test_broken = false, noexist = false;
6161
int rv = 0, errors = 0;
62+
struct stat st;
63+
mode_t mode;
64+
uid_t uid;
65+
gid_t gid;
6266

6367
array = xbps_dictionary_get(pkg_filesd, "files");
6468
if (array != NULL && xbps_array_count(array) > 0) {
@@ -67,6 +71,7 @@ check_pkg_files(struct xbps_handle *xhp, const char *pkgname, void *arg)
6771
return -1;
6872

6973
while ((obj = xbps_object_iterator_next(iter))) {
74+
noexist = false;
7075
xbps_dictionary_get_cstring_nocopy(obj, "file", &file);
7176
/* skip noextract files */
7277
if (xhp->noextract && xbps_patterns_match(xhp->noextract, file))
@@ -77,13 +82,11 @@ check_pkg_files(struct xbps_handle *xhp, const char *pkgname, void *arg)
7782
rv = xbps_file_sha256_check(path, sha256);
7883
switch (rv) {
7984
case 0:
80-
free(path);
8185
break;
8286
case ENOENT:
8387
xbps_error_printf("%s: unexistent file %s.\n",
8488
pkgname, file);
85-
free(path);
86-
test_broken = true;
89+
test_broken = noexist = true;
8790
break;
8891
case ERANGE:
8992
mutable = false;
@@ -94,17 +97,47 @@ check_pkg_files(struct xbps_handle *xhp, const char *pkgname, void *arg)
9497
"for %s.\n", pkgname, file);
9598
test_broken = true;
9699
}
97-
free(path);
98100
break;
99101
default:
100102
xbps_error_printf(
101103
"%s: can't check `%s' (%s)\n",
102104
pkgname, file, strerror(rv));
103-
free(path);
104105
break;
105106
}
106-
}
107-
xbps_object_iterator_release(iter);
107+
if (!noexist) {
108+
mode = uid = gid = 0;
109+
if (stat(path, &st) == -1) {
110+
rv = errno;
111+
xbps_error_printf(
112+
"%s: can't check `%s' (%s)\n",
113+
pkgname, file, strerror(rv));
114+
} else {
115+
if (xbps_dictionary_get_uint32(obj, "mode", &mode)) {
116+
if (st.st_mode != mode) {
117+
xbps_error_printf("%s: mode mismatch "
118+
"for %s.\n", pkgname, file);
119+
test_broken = true;
120+
}
121+
}
122+
if (xbps_dictionary_get_uint32(obj, "uid", &uid)) {
123+
if (st.st_uid != uid) {
124+
xbps_error_printf("%s: owner mismatch "
125+
"for %s.\n", pkgname, file);
126+
test_broken = true;
127+
}
128+
}
129+
if (xbps_dictionary_get_uint32(obj, "gid", &gid)) {
130+
if (st.st_gid != gid) {
131+
xbps_error_printf("%s: group mismatch "
132+
"for %s.\n", pkgname, file);
133+
test_broken = true;
134+
}
135+
}
136+
}
137+
}
138+
free(path);
139+
}
140+
xbps_object_iterator_release(iter);
108141
}
109142
if (test_broken) {
110143
xbps_error_printf("%s: files check FAILED.\n", pkgname);

bin/xbps-pkgdb/check_pkg_symlinks.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ check_pkg_symlinks(struct xbps_handle *xhp, const char *pkgname, void *arg)
5353
xbps_object_t obj;
5454
xbps_dictionary_t filesd = arg;
5555
int rv = 0;
56+
struct stat st;
57+
mode_t mode;
58+
uid_t uid;
59+
gid_t gid;
5660

5761
array = xbps_dictionary_get(filesd, "links");
5862
if (array == NULL)
@@ -92,6 +96,34 @@ check_pkg_symlinks(struct xbps_handle *xhp, const char *pkgname, void *arg)
9296
pkgname, file, lnk, tgt);
9397
rv = -1;
9498
}
99+
if (lstat(path, &st) == -1) {
100+
rv = errno;
101+
xbps_error_printf(
102+
"%s: can't check `%s' (%s)\n",
103+
pkgname, file, strerror(rv));
104+
} else {
105+
if (xbps_dictionary_get_uint32(obj, "mode", &mode)) {
106+
if (st.st_mode != mode) {
107+
xbps_error_printf("%s: mode mismatch "
108+
"for %s.\n", pkgname, file);
109+
rv = -1;
110+
}
111+
}
112+
if (xbps_dictionary_get_uint32(obj, "uid", &uid)) {
113+
if (st.st_uid != uid) {
114+
xbps_error_printf("%s: owner mismatch "
115+
"for %s.\n", pkgname, file);
116+
rv = -1;
117+
}
118+
}
119+
if (xbps_dictionary_get_uint32(obj, "gid", &gid)) {
120+
if (st.st_gid != gid) {
121+
xbps_error_printf("%s: group mismatch "
122+
"for %s.\n", pkgname, file);
123+
rv = -1;
124+
}
125+
}
126+
}
95127
free(lnk);
96128
}
97129
return rv;

0 commit comments

Comments
 (0)