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