@@ -370,47 +370,9 @@ func (h *Host) CreateContainer(ctx context.Context, id string, settings *prot.VM
370370 settings .OCISpecification .Process .Capabilities = capsToKeep
371371 }
372372
373- // Write security policy, signed UVM reference and host AMD certificate to
374- // container's rootfs, so that application and sidecar containers can have
375- // access to it. The security policy is required by containers which need to
376- // extract init-time claims found in the security policy. The directory path
377- // containing the files is exposed via UVM_SECURITY_CONTEXT_DIR env var.
378- // It may be an error to have a security policy but not expose it to the
379- // container as in that case it can never be checked as correct by a verifier.
380373 if oci .ParseAnnotationsBool (ctx , settings .OCISpecification .Annotations , annotations .LCOWSecurityPolicyEnv , true ) {
381- encodedPolicy := h .securityOptions .PolicyEnforcer .EncodedSecurityPolicy ()
382- hostAMDCert := settings .OCISpecification .Annotations [annotations .LCOWHostAMDCertificate ]
383- if len (encodedPolicy ) > 0 || len (hostAMDCert ) > 0 || len (h .securityOptions .UvmReferenceInfo ) > 0 {
384- // Use os.MkdirTemp to make sure that the directory is unique.
385- securityContextDir , err := os .MkdirTemp (settings .OCISpecification .Root .Path , securitypolicy .SecurityContextDirTemplate )
386- if err != nil {
387- return nil , fmt .Errorf ("failed to create security context directory: %w" , err )
388- }
389- // Make sure that files inside directory are readable
390- if err := os .Chmod (securityContextDir , 0755 ); err != nil {
391- return nil , fmt .Errorf ("failed to chmod security context directory: %w" , err )
392- }
393-
394- if len (encodedPolicy ) > 0 {
395- if err := writeFileInDir (securityContextDir , securitypolicy .PolicyFilename , []byte (encodedPolicy ), 0744 ); err != nil {
396- return nil , fmt .Errorf ("failed to write security policy: %w" , err )
397- }
398- }
399- if len (h .securityOptions .UvmReferenceInfo ) > 0 {
400- if err := writeFileInDir (securityContextDir , securitypolicy .ReferenceInfoFilename , []byte (h .securityOptions .UvmReferenceInfo ), 0744 ); err != nil {
401- return nil , fmt .Errorf ("failed to write UVM reference info: %w" , err )
402- }
403- }
404-
405- if len (hostAMDCert ) > 0 {
406- if err := writeFileInDir (securityContextDir , securitypolicy .HostAMDCertFilename , []byte (hostAMDCert ), 0744 ); err != nil {
407- return nil , fmt .Errorf ("failed to write host AMD certificate: %w" , err )
408- }
409- }
410-
411- containerCtxDir := fmt .Sprintf ("/%s" , filepath .Base (securityContextDir ))
412- secCtxEnv := fmt .Sprintf ("UVM_SECURITY_CONTEXT_DIR=%s" , containerCtxDir )
413- settings .OCISpecification .Process .Env = append (settings .OCISpecification .Process .Env , secCtxEnv )
374+ if err := h .securityOptions .WriteSecurityContextDir (settings .OCISpecification ); err != nil {
375+ return nil , fmt .Errorf ("failed to write security context dir: %w" , err )
414376 }
415377 }
416378
@@ -1141,17 +1103,3 @@ func processOCIEnvToParam(envs []string) map[string]string {
11411103func isPrivilegedContainerCreationRequest (ctx context.Context , spec * specs.Spec ) bool {
11421104 return oci .ParseAnnotationsBool (ctx , spec .Annotations , annotations .LCOWPrivileged , false )
11431105}
1144-
1145- func writeFileInDir (dir string , filename string , data []byte , perm os.FileMode ) error {
1146- st , err := os .Stat (dir )
1147- if err != nil {
1148- return err
1149- }
1150-
1151- if ! st .IsDir () {
1152- return fmt .Errorf ("not a directory %q" , dir )
1153- }
1154-
1155- targetFilename := filepath .Join (dir , filename )
1156- return os .WriteFile (targetFilename , data , perm )
1157- }
0 commit comments