Skip to content
This repository was archived by the owner on May 2, 2024. It is now read-only.

Commit 941b339

Browse files
committed
added check_overflow_uid_gid function
1 parent 1866d1f commit 941b339

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

nss/src/cache/mod.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -345,23 +345,34 @@ impl CacheDBBuilder {
345345
Ok(c)
346346
}
347347

348-
fn read_file_as_u32(file_path: &str) -> u32 {
349-
match fs::read_to_string(file_path) {
350-
Ok(contents) => {
351-
match contents.trim().parse::<u32>() {
352-
Ok(num) => num,
353-
Err(err) => {
354-
eprintln!("Parsing to u32 fail: {}", err);
355-
0 // fallback to 0
356-
},
357-
}
358-
},
359-
Err(err) => {
360-
eprintln!("error reading file: {}", err);
361-
0 // fallback to 0
362-
},
363-
}
348+
/// check_overflow_uid_gid checks if numbers provided matches with kernel overflow values
349+
/// this is when we are checking owner of cache db, but are running in a namespace, and false values
350+
/// are handed to us.
351+
fn check_overflow_uid_gid(filestat_uid: u32, filestat_gid: u32) -> bool {
352+
353+
let overflowuid_content = match fs::read_to_string("/proc/sys/kernel/overflowuid") {
354+
Ok(c) => c,
355+
Err(_) => return false,
356+
};
357+
358+
let overflowuid = match overflowuid_content.trim().parse::<u32>() {
359+
Ok(n) => n,
360+
Err(_) => return false,
361+
};
362+
363+
let overflowgid_content = match fs::read_to_string("/proc/sys/kernel/overflowgid") {
364+
Ok(c) => c,
365+
Err(_) => return false,
366+
};
367+
368+
let overflowgid = match overflowgid_content.trim().parse::<u32>() {
369+
Ok(n) => n,
370+
Err(_) => return false,
371+
};
372+
373+
filestat_uid == overflowuid && filestat_gid == overflowgid
364374
}
375+
365376
/// check_file_permissions checks the database files and compares the current ownership and
366377
/// permissions with the expected ones.
367378
fn check_file_permissions(files: &Vec<DbFileInfo>) -> Result<(), CacheError> {
@@ -385,10 +396,8 @@ impl CacheDBBuilder {
385396

386397
// Checks ownership
387398
if stat.uid() != file.expected_uid || stat.gid() != file.expected_gid {
388-
let overflowuid = Self::read_file_as_u32("/proc/sys/kernel/overflowuid");
389-
let overflowgid = Self::read_file_as_u32("/proc/sys/kernel/overflowgid");
390399
// check and don't fail if the file ownership matches kernel overflow uid/gid values
391-
if stat.uid() != overflowuid && stat.gid() != overflowgid {
400+
if ! Self::check_overflow_uid_gid(stat.uid(), stat.gid()) {
392401
return Err(CacheError::DatabaseError(format!(
393402
"invalid ownership for {}, expected {}:{} but got {}:{}",
394403
file.path.to_str().unwrap(),
@@ -397,14 +406,6 @@ impl CacheDBBuilder {
397406
stat.uid(),
398407
stat.gid()
399408
)));
400-
}else{
401-
debug!("unexpected ownership for {}, expected {}:{} but got {}:{}",
402-
file.path.to_str().unwrap(),
403-
file.expected_uid,
404-
file.expected_gid,
405-
stat.uid(),
406-
stat.gid()
407-
);
408409
}
409410
}
410411
}

0 commit comments

Comments
 (0)