| 1 | //! Raw System Call Wrappers |
| 2 | //! |
| 3 | use super::types::{KeyCtlOperation, KeySerialId, KeyType}; |
| 4 | use crate::KeyError; |
| 5 | use alloc::ffi::CString; |
| 6 | use core::ffi::CStr; |
| 7 | |
| 8 | /// add_key() creates or updates a key of the given type and description, instantiates |
| 9 | /// it with the payload of length plen, attaches it to the nominated keyring, and |
| 10 | /// returns the key's serial number. |
| 11 | /// |
| 12 | /// The key may be rejected if the provided data is in the wrong format or it is invalid |
| 13 | /// in some other way. If the destination keyring already contains a key that matches the |
| 14 | /// specified type and description, then, if the key type supports it, that key will be |
| 15 | /// updated rather than a new key being created; if not, a new key (with a different ID) |
| 16 | /// will be created and it will displace the link to the extant key from the keyring. |
| 17 | /// |
| 18 | /// The destination keyring serial number may be that of a valid keyring for which the |
| 19 | /// caller has write permission. Alternatively, it may be one of the following special |
| 20 | /// keyring IDs: |
| 21 | pub(crate) fn add_key( |
| 22 | ktype: KeyType, |
| 23 | keyring: libc::c_ulong, |
| 24 | description: &str, |
| 25 | payload: Option<&[u8]>, |
| 26 | ) -> Result<KeySerialId, KeyError> { |
| 27 | // Perform conversion into a c string |
| 28 | let description = CString::new(description).or(Err(KeyError::InvalidDescription))?; |
| 29 | |
| 30 | // When creating keyrings the payload will be NULL |
| 31 | let (payload, plen) = match payload { |
| 32 | Some(p) => (p.as_ptr(), p.len()), |
| 33 | None => (core::ptr::null(), 0), |
| 34 | }; |
| 35 | |
| 36 | // Perform the actual system call |
| 37 | let res = unsafe { |
| 38 | libc::syscall( |
| 39 | libc::SYS_add_key, |
| 40 | Into::<&'static CStr>::into(ktype).as_ptr(), |
| 41 | description.as_ptr(), |
| 42 | payload, |
| 43 | plen as libc::size_t, |
| 44 | keyring as u32, |
| 45 | ) |
| 46 | }; |
| 47 | |
| 48 | // Return the underlying error |
| 49 | if res < 0 { |
| 50 | return Err(KeyError::from_errno()); |
| 51 | } |
| 52 | |
| 53 | // Otherwise return the ID |
| 54 | Ok(KeySerialId::new( |
| 55 | res.try_into().or(Err(KeyError::InvalidIdentifier))?, |
| 56 | )) |
| 57 | } |
| 58 | |
| 59 | /// request_key() attempts to find a key of the given type with a description that |
| 60 | /// matches the specified description. If such a key could not be found, then |
| 61 | /// the key is optionally created. |
| 62 | /// |
| 63 | /// If the key is found or created, request_key() attaches it to the keyring |
| 64 | /// and returns the key's serial number. |
| 65 | /// |
| 66 | /// request_key() first recursively searches for a matching key in all of the keyrings |
| 67 | /// attached to the calling process. The keyrings are searched in the order: |
| 68 | /// thread-specific keyring, process-specific keyring, and then session keyring. |
| 69 | /// |
| 70 | /// If request_key() is called from a program invoked by request_key() on behalf |
| 71 | /// of some other process to generate a key, then the keyrings of that other process |
| 72 | /// will be searched next, using that other process's user ID, group ID, supplementary |
| 73 | /// group IDs, and security context to determine access. |
| 74 | /// |
| 75 | /// The search of the keyring tree is breadth-first: the keys in each keyring searched |
| 76 | /// are checked for a match before any child keyrings are recursed into. Only keys for |
| 77 | /// which the caller has search permission be found, and only keyrings for which the |
| 78 | /// caller has search permission may be searched. |
| 79 | /// |
| 80 | /// If the key is not found and callout info is empty then the call fails with the |
| 81 | /// error ENOKEY. |
| 82 | /// |
| 83 | /// If the key is not found and callout info is not empty, then the kernel attempts |
| 84 | /// to invoke a user-space program to instantiate the key. |
| 85 | pub(crate) fn request_key( |
| 86 | ktype: KeyType, |
| 87 | keyring: libc::c_ulong, |
| 88 | description: &str, |
| 89 | info: Option<&str>, |
| 90 | ) -> Result<KeySerialId, KeyError> { |
| 91 | // Perform conversion into a c string |
| 92 | let description = CString::new(description).or(Err(KeyError::InvalidDescription))?; |
| 93 | let callout = CString::new(info.unwrap_or("")).or(Err(KeyError::InvalidDescription))?; |
| 94 | |
| 95 | // Perform the actual system call. By setting callout to NULL the kernel will |
| 96 | // not invoke /sbin/request-key |
| 97 | let res = unsafe { |
| 98 | libc::syscall( |
| 99 | libc::SYS_request_key, |
| 100 | Into::<&'static CStr>::into(ktype).as_ptr(), |
| 101 | description.as_ptr(), |
| 102 | info.map_or_else(core::ptr::null, |_| callout.as_ptr()), |
| 103 | keyring as u32, |
| 104 | ) |
| 105 | }; |
| 106 | |
| 107 | // Return the underlying error |
| 108 | if res < 0 { |
| 109 | return Err(KeyError::from_errno()); |
| 110 | } |
| 111 | |
| 112 | // Otherwise return the ID |
| 113 | Ok(KeySerialId::new( |
| 114 | res.try_into().or(Err(KeyError::InvalidIdentifier))?, |
| 115 | )) |
| 116 | } |
| 117 | |
| 118 | /// keyctl() allows user-space programs to perform key manipulation. |
| 119 | /// |
| 120 | /// The operation performed by keyctl() is determined by the value of the operation argument. |
| 121 | /// Each of these operations is wrapped by the KeyCtl interface (provided by the this crate) |
| 122 | /// into individual functions (noted below) to permit the compiler to check types. |
| 123 | pub(crate) fn keyctl_impl( |
| 124 | operation: KeyCtlOperation, |
| 125 | arg2: libc::c_ulong, |
| 126 | arg3: libc::c_ulong, |
| 127 | arg4: libc::c_ulong, |
| 128 | arg5: libc::c_ulong, |
| 129 | ) -> Result<libc::c_long, KeyError> { |
| 130 | // Perform the actual system call |
| 131 | let res = unsafe { libc::syscall(libc::SYS_keyctl, operation as u32, arg2, arg3, arg4, arg5) }; |
| 132 | |
| 133 | // Return the underlying error |
| 134 | if res < 0 { |
| 135 | return Err(KeyError::from_errno()); |
| 136 | } |
| 137 | |
| 138 | // Otherwise return the result |
| 139 | Ok(res) |
| 140 | } |