Skip to content

Commit bddf0ec

Browse files
committed
keyring: support restriction rules for keyrings
Fixes: #11
1 parent 58ff948 commit bddf0ec

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

keyutils-raw/src/functions.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,38 @@ pub fn keyctl_dh_compute(
378378
}
379379
.map(size)
380380
}
381+
382+
pub enum Restriction<'a> {
383+
AllLinks,
384+
ByType {
385+
type_: &'a str,
386+
restriction: &'a str,
387+
},
388+
}
389+
390+
pub fn keyctl_restrict_keyring(keyring: KeyringSerial, restriction: Restriction) -> Result<()> {
391+
let type_cstr;
392+
let restriction_cstr;
393+
394+
let (type_ptr, restriction_ptr) = match restriction {
395+
Restriction::AllLinks => (ptr::null(), ptr::null()),
396+
Restriction::ByType {
397+
type_,
398+
restriction,
399+
} => {
400+
type_cstr = cstring(type_);
401+
restriction_cstr = cstring(restriction);
402+
403+
(type_cstr.as_ptr(), restriction_cstr.as_ptr())
404+
},
405+
};
406+
unsafe {
407+
keyctl!(
408+
libc::KEYCTL_RESTRICT_KEYRING,
409+
keyring.get(),
410+
type_ptr,
411+
restriction_ptr,
412+
)
413+
}
414+
.map(ignore)
415+
}

src/api.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,32 @@ impl Keyring {
403403
keyctl_setperm(self.id, perms)
404404
}
405405

406+
/// Restrict all links into the keyring.
407+
///
408+
/// Requires the `setattr` permission on the keyring and the SysAdmin capability to change it to
409+
/// anything other than the current user.
410+
pub fn restrict_all(&mut self) -> Result<()> {
411+
keyctl_restrict_keyring(self.id, Restriction::AllLinks)
412+
}
413+
414+
/// Restrict links into the keyring.
415+
///
416+
/// Requires the `setattr` permission on the keyring and the SysAdmin capability to change it to
417+
/// anything other than the current user.
418+
pub fn restrict_by_type<K, R>(&mut self, restriction: R) -> Result<()>
419+
where
420+
K: RestrictableKeyType,
421+
R: Borrow<K::Restriction>,
422+
{
423+
keyctl_restrict_keyring(
424+
self.id,
425+
Restriction::ByType {
426+
type_: K::name(),
427+
restriction: &restriction.borrow().restriction(),
428+
},
429+
)
430+
}
431+
406432
fn description_raw(&self) -> Result<String> {
407433
// Get the size of the description.
408434
let mut sz = keyctl_describe(self.id, None)?;

src/keytype.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,27 @@ impl KeyPayload for Vec<u8> {
9090
Cow::Borrowed(&self)
9191
}
9292
}
93+
94+
/// A key which may be restricted into being added to a keyring.
95+
pub trait RestrictableKeyType: KeyType {
96+
/// The type for representing a restriction for adding keys of this type.
97+
type Restriction: KeyRestriction + ?Sized;
98+
}
99+
100+
/// A restriction for a key.
101+
pub trait KeyRestriction {
102+
/// The restriction string of the key.
103+
fn restriction(&self) -> Cow<str>;
104+
}
105+
106+
impl KeyRestriction for str {
107+
fn restriction(&self) -> Cow<str> {
108+
Cow::Borrowed(&self)
109+
}
110+
}
111+
112+
impl KeyRestriction for String {
113+
fn restriction(&self) -> Cow<str> {
114+
Cow::Borrowed(&self)
115+
}
116+
}

0 commit comments

Comments
 (0)