2019-05-15 13:27:34 +00:00
|
|
|
//! Wrapper functions for the libc xattr calls
|
|
|
|
|
2020-04-24 08:48:28 +00:00
|
|
|
use std::ffi::CStr;
|
2019-05-15 13:27:34 +00:00
|
|
|
use std::os::unix::io::RawFd;
|
2020-04-24 08:48:28 +00:00
|
|
|
|
2019-05-15 13:27:34 +00:00
|
|
|
use nix::errno::Errno;
|
2019-07-01 09:03:25 +00:00
|
|
|
|
2020-04-24 09:14:43 +00:00
|
|
|
use proxmox::c_str;
|
2019-07-01 09:03:25 +00:00
|
|
|
use proxmox::tools::vec;
|
|
|
|
|
2020-04-24 09:28:03 +00:00
|
|
|
/// `"security.capability"` as a CStr to avoid typos.
|
2020-04-24 09:14:43 +00:00
|
|
|
///
|
|
|
|
/// This cannot be `const` until `const_cstr_unchecked` is stable.
|
|
|
|
#[inline]
|
2020-04-24 09:28:03 +00:00
|
|
|
pub fn xattr_name_fcaps() -> &'static CStr {
|
2020-04-24 09:14:43 +00:00
|
|
|
c_str!("security.capability")
|
|
|
|
}
|
2019-05-15 13:27:34 +00:00
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
/// `"system.posix_acl_access"` as a CStr to avoid typos.
|
|
|
|
///
|
|
|
|
/// This cannot be `const` until `const_cstr_unchecked` is stable.
|
|
|
|
#[inline]
|
|
|
|
pub fn xattr_acl_access() -> &'static CStr {
|
|
|
|
c_str!("system.posix_acl_access")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `"system.posix_acl_default"` as a CStr to avoid typos.
|
|
|
|
///
|
|
|
|
/// This cannot be `const` until `const_cstr_unchecked` is stable.
|
|
|
|
#[inline]
|
|
|
|
pub fn xattr_acl_default() -> &'static CStr {
|
|
|
|
c_str!("system.posix_acl_default")
|
|
|
|
}
|
|
|
|
|
2020-04-24 08:48:28 +00:00
|
|
|
/// Result of `flistxattr`, allows iterating over the attributes as a list of `&CStr`s.
|
|
|
|
///
|
|
|
|
/// Listing xattrs produces a list separated by zeroes, inherently making them available as `&CStr`
|
|
|
|
/// already, so we make use of this fact and reflect this in the interface.
|
|
|
|
pub struct ListXAttr {
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ListXAttr {
|
|
|
|
fn new(data: Vec<u8>) -> Self {
|
|
|
|
Self { data }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IntoIterator for &'a ListXAttr {
|
|
|
|
type Item = &'a CStr;
|
|
|
|
type IntoIter = ListXAttrIter<'a>;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
ListXAttrIter {
|
|
|
|
data: &self.data,
|
|
|
|
at: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterator over the extended attribute entries in a `ListXAttr`.
|
|
|
|
pub struct ListXAttrIter<'a> {
|
|
|
|
data: &'a [u8],
|
|
|
|
at: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for ListXAttrIter<'a> {
|
|
|
|
type Item = &'a CStr;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<&'a CStr> {
|
|
|
|
let data = &self.data[self.at..];
|
|
|
|
let next = data.iter().position(|b| *b == 0)? + 1;
|
|
|
|
self.at += next;
|
|
|
|
Some(unsafe { CStr::from_bytes_with_nul_unchecked(&data[..next]) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a list of extended attributes accessible as an iterator over items of type `&CStr`.
|
|
|
|
pub fn flistxattr(fd: RawFd) -> Result<ListXAttr, nix::errno::Errno> {
|
2019-05-15 13:27:34 +00:00
|
|
|
// Initial buffer size for the attribute list, if content does not fit
|
|
|
|
// it gets dynamically increased until big enough.
|
|
|
|
let mut size = 256;
|
2019-05-22 13:02:16 +00:00
|
|
|
let mut buffer = vec::undefined(size);
|
2019-05-15 13:27:34 +00:00
|
|
|
let mut bytes = unsafe {
|
2020-07-19 17:42:13 +00:00
|
|
|
libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len())
|
2019-05-15 13:27:34 +00:00
|
|
|
};
|
|
|
|
while bytes < 0 {
|
|
|
|
let err = Errno::last();
|
|
|
|
match err {
|
|
|
|
Errno::ERANGE => {
|
|
|
|
// Buffer was not big enough to fit the list, retry with double the size
|
2020-04-24 07:58:15 +00:00
|
|
|
size = size.checked_mul(2).ok_or(Errno::ENOMEM)?;
|
2019-05-15 13:27:34 +00:00
|
|
|
},
|
|
|
|
_ => return Err(err),
|
|
|
|
}
|
|
|
|
// Retry to read the list with new buffer
|
|
|
|
buffer.resize(size, 0);
|
|
|
|
bytes = unsafe {
|
2020-07-19 17:42:13 +00:00
|
|
|
libc::flistxattr(fd, buffer.as_mut_ptr() as *mut libc::c_char, buffer.len())
|
2019-05-15 13:27:34 +00:00
|
|
|
};
|
|
|
|
}
|
2020-04-24 08:48:28 +00:00
|
|
|
buffer.truncate(bytes as usize);
|
2019-05-15 13:27:34 +00:00
|
|
|
|
2020-04-24 08:48:28 +00:00
|
|
|
Ok(ListXAttr::new(buffer))
|
2019-05-15 13:27:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 08:48:28 +00:00
|
|
|
/// Get an extended attribute by name.
|
|
|
|
///
|
|
|
|
/// Extended attributes may not contain zeroes, which we enforce in the API by using a `&CStr`
|
|
|
|
/// type.
|
|
|
|
pub fn fgetxattr(fd: RawFd, name: &CStr) -> Result<Vec<u8>, nix::errno::Errno> {
|
2019-05-15 13:27:34 +00:00
|
|
|
let mut size = 256;
|
2019-05-22 13:02:16 +00:00
|
|
|
let mut buffer = vec::undefined(size);
|
2019-05-15 13:27:34 +00:00
|
|
|
let mut bytes = unsafe {
|
2020-04-24 08:48:28 +00:00
|
|
|
libc::fgetxattr(fd, name.as_ptr(), buffer.as_mut_ptr() as *mut core::ffi::c_void, buffer.len())
|
2019-05-15 13:27:34 +00:00
|
|
|
};
|
|
|
|
while bytes < 0 {
|
|
|
|
let err = Errno::last();
|
|
|
|
match err {
|
|
|
|
Errno::ERANGE => {
|
|
|
|
// Buffer was not big enough to fit the value, retry with double the size
|
2020-04-24 07:58:15 +00:00
|
|
|
size = size.checked_mul(2).ok_or(Errno::ENOMEM)?;
|
2019-05-15 13:27:34 +00:00
|
|
|
},
|
|
|
|
_ => return Err(err),
|
|
|
|
}
|
|
|
|
buffer.resize(size, 0);
|
|
|
|
bytes = unsafe {
|
2020-07-19 17:42:13 +00:00
|
|
|
libc::fgetxattr(fd, name.as_ptr() as *const libc::c_char, buffer.as_mut_ptr() as *mut core::ffi::c_void, buffer.len())
|
2019-05-15 13:27:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
buffer.resize(bytes as usize, 0);
|
|
|
|
|
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:14:43 +00:00
|
|
|
/// Set an extended attribute on a file descriptor.
|
|
|
|
pub fn fsetxattr(fd: RawFd, name: &CStr, data: &[u8]) -> Result<(), nix::errno::Errno> {
|
2019-05-15 13:27:34 +00:00
|
|
|
let flags = 0 as libc::c_int;
|
|
|
|
let result = unsafe {
|
2020-04-24 09:14:43 +00:00
|
|
|
libc::fsetxattr(fd, name.as_ptr(), data.as_ptr() as *const libc::c_void, data.len(), flags)
|
2019-05-15 13:27:34 +00:00
|
|
|
};
|
|
|
|
if result < 0 {
|
2020-04-24 09:14:43 +00:00
|
|
|
return Err(Errno::last());
|
2019-05-15 13:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:14:43 +00:00
|
|
|
pub fn fsetxattr_fcaps(fd: RawFd, fcaps: &[u8]) -> Result<(), nix::errno::Errno> {
|
2019-05-15 13:27:34 +00:00
|
|
|
// TODO casync checks and removes capabilities if they are set
|
2020-04-24 09:14:43 +00:00
|
|
|
fsetxattr(fd, xattr_name_fcaps(), fcaps)
|
2019-05-15 13:27:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 08:48:28 +00:00
|
|
|
pub fn is_security_capability(name: &CStr) -> bool {
|
2020-04-24 09:14:43 +00:00
|
|
|
name.to_bytes() == xattr_name_fcaps().to_bytes()
|
2019-05-17 12:23:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
pub fn is_acl(name: &CStr) -> bool {
|
|
|
|
name.to_bytes() == xattr_acl_access().to_bytes()
|
|
|
|
|| name.to_bytes() == xattr_acl_default().to_bytes()
|
|
|
|
}
|
|
|
|
|
2019-05-23 09:58:40 +00:00
|
|
|
/// Check if the passed name buffer starts with a valid xattr namespace prefix
|
|
|
|
/// and is within the length limit of 255 bytes
|
2020-04-24 08:48:28 +00:00
|
|
|
pub fn is_valid_xattr_name(c_name: &CStr) -> bool {
|
|
|
|
let name = c_name.to_bytes();
|
2019-05-23 09:58:40 +00:00
|
|
|
if name.is_empty() || name.len() > 255 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if name.starts_with(b"user.") || name.starts_with(b"trusted.") {
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-20 09:19:53 +00:00
|
|
|
// samba saves windows ACLs there
|
|
|
|
if name == b"security.NTACL" {
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-24 08:48:28 +00:00
|
|
|
is_security_capability(c_name)
|
2019-05-17 12:23:33 +00:00
|
|
|
}
|
2019-05-21 12:58:05 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-04-24 09:14:43 +00:00
|
|
|
use std::ffi::CString;
|
2019-05-21 12:58:05 +00:00
|
|
|
use std::fs::OpenOptions;
|
|
|
|
use std::os::unix::io::AsRawFd;
|
2020-04-24 09:14:43 +00:00
|
|
|
|
2019-05-21 12:58:05 +00:00
|
|
|
use nix::errno::Errno;
|
|
|
|
|
2020-04-24 09:14:43 +00:00
|
|
|
use proxmox::c_str;
|
|
|
|
|
2019-05-21 12:58:05 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fsetxattr_fgetxattr() {
|
|
|
|
let path = "./tests/xattrs.txt";
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.open(&path)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let fd = file.as_raw_fd();
|
|
|
|
|
|
|
|
let mut name = b"user.".to_vec();
|
|
|
|
for _ in 0..260 {
|
|
|
|
name.push(b'a');
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:14:43 +00:00
|
|
|
let invalid_name = CString::new(name).unwrap();
|
2019-05-21 12:58:05 +00:00
|
|
|
|
2020-04-24 09:14:43 +00:00
|
|
|
assert!(fsetxattr(fd, c_str!("user.attribute0"), b"value0").is_ok());
|
|
|
|
assert!(fsetxattr(fd, c_str!("user.empty"), b"").is_ok());
|
2019-06-05 05:57:42 +00:00
|
|
|
|
|
|
|
if nix::unistd::Uid::current() != nix::unistd::ROOT {
|
2020-04-24 09:14:43 +00:00
|
|
|
assert_eq!(fsetxattr(fd, c_str!("trusted.attribute0"), b"value0"), Err(Errno::EPERM));
|
2019-06-05 05:57:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 09:14:43 +00:00
|
|
|
assert_eq!(fsetxattr(fd, c_str!("garbage.attribute0"), b"value"), Err(Errno::EOPNOTSUPP));
|
|
|
|
assert_eq!(fsetxattr(fd, &invalid_name, b"err"), Err(Errno::ERANGE));
|
2019-05-21 12:58:05 +00:00
|
|
|
|
2020-04-24 08:48:28 +00:00
|
|
|
let v0 = fgetxattr(fd, c_str!("user.attribute0")).unwrap();
|
|
|
|
let v1 = fgetxattr(fd, c_str!("user.empty")).unwrap();
|
2019-05-21 12:58:05 +00:00
|
|
|
|
|
|
|
assert_eq!(v0, b"value0".as_ref());
|
|
|
|
assert_eq!(v1, b"".as_ref());
|
2020-04-24 08:48:28 +00:00
|
|
|
assert_eq!(fgetxattr(fd, c_str!("user.attribute1")), Err(Errno::ENODATA));
|
2019-05-21 12:58:05 +00:00
|
|
|
|
|
|
|
std::fs::remove_file(&path).unwrap();
|
|
|
|
}
|
2019-05-23 10:14:22 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_valid_xattr_name() {
|
2020-04-24 08:48:28 +00:00
|
|
|
let too_long = CString::new(vec![b'a'; 265]).unwrap();
|
|
|
|
|
|
|
|
assert!(!is_valid_xattr_name(&too_long));
|
|
|
|
assert!(!is_valid_xattr_name(c_str!("system.attr")));
|
|
|
|
assert!(is_valid_xattr_name(c_str!("user.attr")));
|
|
|
|
assert!(is_valid_xattr_name(c_str!("trusted.attr")));
|
2020-04-24 09:14:43 +00:00
|
|
|
assert!(is_valid_xattr_name(super::xattr_name_fcaps()));
|
2019-05-23 10:14:22 +00:00
|
|
|
}
|
2019-05-21 12:58:05 +00:00
|
|
|
}
|