@@ -100,6 +100,7 @@ pub struct Command {
100100 uid : Option < uid_t > ,
101101 gid : Option < gid_t > ,
102102 saw_nul : bool ,
103+ saw_invalid_env_key : bool ,
103104 closures : Vec < Box < dyn FnMut ( ) -> io:: Result < ( ) > + Send + Sync > > ,
104105 groups : Option < Box < [ gid_t ] > > ,
105106 stdin : Option < Stdio > ,
@@ -193,6 +194,7 @@ impl Command {
193194 uid : None ,
194195 gid : None ,
195196 saw_nul,
197+ saw_invalid_env_key : false ,
196198 closures : Vec :: new ( ) ,
197199 groups : None ,
198200 stdin : None ,
@@ -217,6 +219,7 @@ impl Command {
217219 uid : None ,
218220 gid : None ,
219221 saw_nul,
222+ saw_invalid_env_key : false ,
220223 closures : Vec :: new ( ) ,
221224 groups : None ,
222225 stdin : None ,
@@ -279,8 +282,18 @@ impl Command {
279282 self . create_pidfd
280283 }
281284
282- pub fn saw_nul ( & self ) -> bool {
283- self . saw_nul
285+ pub fn validate_input ( & self ) -> io:: Result < ( ) > {
286+ if self . saw_invalid_env_key {
287+ Err ( io:: const_io_error!( io:: ErrorKind :: InvalidInput , "env key empty or equals sign found in env key" ) )
288+ } else if self . saw_nul {
289+ Err ( io:: const_io_error!( io:: ErrorKind :: InvalidInput , "nul byte found in provided data" ) )
290+ } else {
291+ Ok ( ( ) )
292+ }
293+ }
294+
295+ pub fn saw_invalid_env_key ( & self ) -> bool {
296+ self . saw_invalid_env_key
284297 }
285298
286299 pub fn get_program ( & self ) -> & OsStr {
@@ -361,7 +374,7 @@ impl Command {
361374
362375 pub fn capture_env ( & mut self ) -> Option < CStringArray > {
363376 let maybe_env = self . env . capture_if_changed ( ) ;
364- maybe_env. map ( |env| construct_envp ( env, & mut self . saw_nul ) )
377+ maybe_env. map ( |env| construct_envp ( env, & mut self . saw_nul , & mut self . saw_invalid_env_key ) )
365378 }
366379
367380 #[ allow( dead_code) ]
@@ -426,9 +439,21 @@ impl CStringArray {
426439 }
427440}
428441
429- fn construct_envp ( env : BTreeMap < OsString , OsString > , saw_nul : & mut bool ) -> CStringArray {
442+ fn construct_envp ( env : BTreeMap < OsString , OsString > , saw_nul : & mut bool , saw_invalid_env_key : & mut bool ) -> CStringArray {
430443 let mut result = CStringArray :: with_capacity ( env. len ( ) ) ;
431444 for ( mut k, v) in env {
445+ {
446+ let mut iter = k. as_bytes ( ) . iter ( ) ;
447+ if iter. next ( ) . is_none ( ) {
448+ * saw_invalid_env_key = true ;
449+ continue ;
450+ }
451+ if iter. any ( |& b| b == b'=' ) {
452+ * saw_invalid_env_key = true ;
453+ continue ;
454+ }
455+ }
456+
432457 // Reserve additional space for '=' and null terminator
433458 k. reserve_exact ( v. len ( ) + 2 ) ;
434459 k. push ( "=" ) ;
0 commit comments