1#![allow(missing_docs)]
44
45use crate::bytecode::{Cap, CapRights, NativeMask, RevocationCell};
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum DelegateError {
49 SourceRevoked,
50 SourceLacksNative,
54}
55
56impl std::fmt::Display for DelegateError {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 match self {
59 DelegateError::SourceRevoked => f.write_str("delegate: source capability revoked"),
60 DelegateError::SourceLacksNative => {
61 f.write_str("delegate: cannot specify native-mask attenuation without NATIVE right")
62 }
63 }
64 }
65}
66
67impl std::error::Error for DelegateError {}
68
69pub fn exec_delegate(
73 src: &Cap,
74 src_cell: &RevocationCell,
75 want_rights: CapRights,
76 want_native: Option<&NativeMask>,
77) -> Result<Cap, DelegateError> {
78 if !src.is_valid(src_cell) {
79 return Err(DelegateError::SourceRevoked);
80 }
81 if want_native.is_some() && !src.rights().contains(CapRights::NATIVE) {
82 return Err(DelegateError::SourceLacksNative);
83 }
84 Ok(src.attenuate(want_rights, want_native))
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90 use crate::bytecode::{CapTarget, NativeMask, RevocationCell};
91
92 #[test]
93 fn cannot_escalate_via_delegate_regardless_of_requested_mask() {
94 let cell = RevocationCell::new();
95 let src = Cap::root(CapTarget::Flow(1), CapRights::SEND, None, &cell);
96 let out = match exec_delegate(
97 &src,
98 &cell,
99 CapRights::SEND
100 .union(CapRights::ADMIN)
101 .union(CapRights::NATIVE),
102 None,
103 ) {
104 Ok(c) => c,
105 Err(err) => panic!("delegate of live SEND cap must succeed: {err}"),
106 };
107 assert!(out.rights().contains(CapRights::SEND));
108 assert!(!out.rights().contains(CapRights::ADMIN));
109 assert!(!out.rights().contains(CapRights::NATIVE));
110 assert!(out.rights().is_subset_of(src.rights()));
111 }
112
113 #[test]
114 fn rights_result_is_always_intersection() {
115 let cell = RevocationCell::new();
116 for src_bits in 0u32..=255 {
117 for want_bits in 0u32..=255 {
118 let src = Cap::root(
119 CapTarget::Flow(1),
120 CapRights::from_bits(src_bits),
121 None,
122 &cell,
123 );
124 let out = match exec_delegate(&src, &cell, CapRights::from_bits(want_bits), None) {
125 Ok(c) => c,
126 Err(err) => panic!("live cap must delegate: {err}"),
127 };
128 assert_eq!(out.rights().bits(), src_bits & want_bits);
129 assert!(out.rights().is_subset_of(src.rights()));
130 }
131 }
132 }
133
134 #[test]
135 fn native_mask_can_only_be_narrowed() {
136 let cell = RevocationCell::new();
137 let src_mask = NativeMask::from_indices(64, &[1, 3, 5, 7]);
138 let requested = NativeMask::from_indices(64, &[3, 5, 9, 11]); let src = Cap::root(
140 CapTarget::Flow(1),
141 CapRights::NATIVE,
142 Some(src_mask.clone()),
143 &cell,
144 );
145 let out = match exec_delegate(&src, &cell, CapRights::NATIVE, Some(&requested)) {
146 Ok(cap) => cap,
147 Err(err) => panic!("live NATIVE capability should delegate: {err}"),
148 };
149 let got = match out.native_mask() {
150 Some(m) => m,
151 None => panic!("native mask present"),
152 };
153 assert!(got.is_subset_of(&src_mask));
154 assert!(got.allows(3));
155 assert!(got.allows(5));
156 assert!(!got.allows(1)); assert!(!got.allows(9)); assert!(!got.allows(11));
159 }
160
161 #[test]
162 fn cannot_delegate_native_from_non_native_source() {
163 let cell = RevocationCell::new();
164 let src = Cap::root(CapTarget::Flow(1), CapRights::SEND, None, &cell);
165 let requested = NativeMask::from_indices(32, &[0, 1]);
166 assert!(matches!(
167 exec_delegate(&src, &cell, CapRights::SEND, Some(&requested)),
168 Err(DelegateError::SourceLacksNative)
169 ));
170 }
171
172 #[test]
173 fn want_native_none_keeps_source_mask() {
174 let cell = RevocationCell::new();
175 let src_mask = NativeMask::from_indices(32, &[2, 4]);
176 let src = Cap::root(
177 CapTarget::Flow(1),
178 CapRights::NATIVE.union(CapRights::SEND),
179 Some(src_mask.clone()),
180 &cell,
181 );
182 let out = match exec_delegate(&src, &cell, CapRights::SEND, None) {
183 Ok(c) => c,
184 Err(err) => panic!("{err}"),
185 };
186 assert!(!out.rights().contains(CapRights::NATIVE));
189 let got = match out.native_mask() {
190 Some(m) => m,
191 None => panic!("mask preserved"),
192 };
193 assert!(got.is_subset_of(&src_mask));
194 assert_eq!(got.allows(2), src_mask.allows(2));
195 assert_eq!(got.allows(4), src_mask.allows(4));
196 }
197
198 #[test]
199 fn delegated_cap_shares_revocation_cell_epoch() {
200 let cell = RevocationCell::new();
201 let src = Cap::root(
202 CapTarget::Flow(1),
203 CapRights::SEND.union(CapRights::NATIVE),
204 Some(NativeMask::from_indices(8, &[0])),
205 &cell,
206 );
207 let child = match exec_delegate(&src, &cell, CapRights::SEND, None) {
208 Ok(c) => c,
209 Err(err) => panic!("{err}"),
210 };
211 assert_eq!(child.epoch(), src.epoch());
212 assert!(child.is_valid(&cell));
213 cell.revoke();
214 assert!(!src.is_valid(&cell));
215 assert!(!child.is_valid(&cell));
216 }
217
218 #[test]
219 fn revoked_source_cannot_delegate() {
220 let cell = RevocationCell::new();
221 let src = Cap::root(CapTarget::Flow(1), CapRights::SEND, None, &cell);
222 cell.revoke();
223 assert!(matches!(
224 exec_delegate(&src, &cell, CapRights::SEND, None),
225 Err(DelegateError::SourceRevoked)
226 ));
227 }
228}