@@ -23,6 +23,12 @@ static EPHEMERAL_MNT: &str = ".ephemeral";
2323static RAID_DEVICE_DIR : & str = "/dev/md/" ;
2424static RAID_DEVICE_NAME : & str = "ephemeral" ;
2525
26+ pub struct BindDirs {
27+ pub allowed_exact : HashSet < & ' static str > ,
28+ pub allowed_prefixes : HashSet < & ' static str > ,
29+ pub disallowed_contains : HashSet < & ' static str > ,
30+ }
31+
2632/// initialize prepares the ephemeral storage for formatting and formats it. For multiple disks
2733/// preparation is the creation of a RAID0 array, for a single disk this is a no-op. The array or disk
2834/// is then formatted with the specified filesystem (default=xfs) if not formatted already.
@@ -116,8 +122,17 @@ pub fn bind(variant: &str, dirs: Vec<String>) -> Result<()> {
116122 let mount_point = Path :: new ( & mount_point) ;
117123 let allowed_dirs = allowed_bind_dirs ( variant) ;
118124 for dir in & dirs {
125+ let exact_match = allowed_dirs. allowed_exact . contains ( dir. as_str ( ) ) ;
126+ let prefix_match = allowed_dirs
127+ . allowed_prefixes
128+ . iter ( )
129+ . any ( |prefix| dir. starts_with ( prefix) ) ;
130+ let disallowed_match = allowed_dirs
131+ . disallowed_contains
132+ . iter ( )
133+ . any ( |contains| dir. contains ( contains) ) ;
119134 ensure ! (
120- allowed_dirs . contains ( dir . as_str ( ) ) ,
135+ exact_match || ( prefix_match && !disallowed_match ) ,
121136 error:: InvalidParameterSnafu {
122137 parameter: dir,
123138 reason: "specified bind directory not in allow list" ,
@@ -269,18 +284,26 @@ pub fn ephemeral_devices() -> Result<Vec<String>> {
269284}
270285
271286/// allowed_bind_dirs returns a set of the directories that can be bound to ephemeral storage, which
272- /// varies based on the variant
273- pub fn allowed_bind_dirs ( variant : & str ) -> HashSet < & ' static str > {
274- let mut allowed = HashSet :: from ( [ "/var/lib/containerd" , "/var/lib/host-containerd" ] ) ;
287+ /// varies based on the variant, a set of the prefixes of directories that are allowed to be bound.
288+ /// and a set of substrings that are disallowed in the directory name.
289+ pub fn allowed_bind_dirs ( variant : & str ) -> BindDirs {
290+ let mut allowed_exact = HashSet :: from ( [ "/var/lib/containerd" , "/var/lib/host-containerd" ] ) ;
275291 if variant. contains ( "k8s" ) {
276- allowed . insert ( "/var/lib/kubelet" ) ;
277- allowed . insert ( "/var/log/pods" ) ;
292+ allowed_exact . insert ( "/var/lib/kubelet" ) ;
293+ allowed_exact . insert ( "/var/log/pods" ) ;
278294 }
279295 if variant. contains ( "ecs" ) {
280- allowed. insert ( "/var/lib/docker" ) ;
281- allowed. insert ( "/var/log/ecs" ) ;
296+ allowed_exact. insert ( "/var/lib/docker" ) ;
297+ allowed_exact. insert ( "/var/log/ecs" ) ;
298+ }
299+ let allowed_prefixes = HashSet :: from ( [ "/mnt/" ] ) ;
300+ let disallowed_contains = HashSet :: from ( [ ".." , "/mnt/.ephemeral" ] ) ;
301+
302+ BindDirs {
303+ allowed_exact,
304+ allowed_prefixes,
305+ disallowed_contains,
282306 }
283- allowed
284307}
285308
286309/// scans the raid array to identify if it has been created already
0 commit comments