Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CP-30434: Save XAPI uefi_certificates to varstored directory if the certificate files don't already exist there and parse secureboot=auto #3813

Merged
merged 1 commit into from
Feb 22, 2019

Conversation

rwdrich
Copy link
Contributor

@rwdrich rwdrich commented Feb 5, 2019

No description provided.

@coveralls
Copy link

coveralls commented Feb 5, 2019

Coverage Status

Coverage decreased (-0.003%) to 21.01% when pulling 355804a on rwdrich:private/richarddav/CP-30434 into a6a1666 on xapi-project:uefi.


(* Check to see if we're using correct device-model when vm has VUSBs*)
if not (List.exists (fun e -> e = "PK.auth") (Array.to_list (Sys.readdir "/usr/share/varstored"))) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Array.exists directly.

ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
@rwdrich
Copy link
Contributor Author

rwdrich commented Feb 6, 2019

Don't merge. Need to move away from deprecated package

ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
Copy link
Contributor

@edwintorok edwintorok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to extract the tarfile

ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
let contents = (Xapi_stdext_base64.Base64.decode (Db.Pool.get_uefi_certificates ~__context ~self)) in
Unixext.write_string_to_file filename contents;
Tar_unix.Archive.extract (fun _ -> !Xapi_globs.varstore_dir) file;
Unix.close file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to use finally to make sure this close is always executed.

let save_uefi_certificates_to_dir ~__context ~self =
let dir = try Sys.readdir !Xapi_globs.varstore_dir
with Sys_error _ ->
Xapi_stdext_unix.Unixext.mkdir_rec "/usr/share/varstored" 0o755;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could still fail if /usr/share/varstored is not a directory but an existing file. It might be worth checking the exception more carefully and to fail in such a case.

with _ ->
debug "Error writing uefi certificates to file";
begin
let _dir = Sys.readdir !Xapi_globs.varstore_dir in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use Sys.file_exists ... && Sys.is_directory ... instead of waiting for exception

Tar_unix.Archive.extract (fun _ -> !Xapi_globs.varstore_dir) file;
finally (fun () -> Unix.close file) (fun () -> ())
end
with _ ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to know what the error was, there is a D.log_and_ignore_exn you could use for that.
Alternatively you can call D.log_backtrace () and log (Printexc.to_string e)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This with doesn't exist anymore by changing to if's above

let contents = (Xapi_stdext_base64.Base64.decode (Db.Pool.get_uefi_certificates ~__context ~self)) in
Unixext.write_string_to_file filename contents;
Tar_unix.Archive.extract (fun _ -> !Xapi_globs.varstore_dir) file;
finally (fun () -> Unix.close file) (fun () -> ())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The finally should wrap everything right after you opened the file: any of them can raise an exception, and you'd leak a file descriptor. As it is written the finally here is a noop.
Would be better if you used Unixext.with_file instead which already does the right thing internally.

with Sys_error _ ->
debug "varstored directory does not exist";
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to keep track of whether this function succeeded or not, either by making it return true/false, or (re)raising an exception when something fails.
For this code it is probably better to reraise the exception, e.g. failwith "Failed to save UEFI certificates: varstored directory does not exist". I think this will get converted to an internal error at the API level, probably worth double checking by triggerring the failure on purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I want to reraise the exception. What's the expected path for a non-UEFI host? Should there be a condition before the function call, or should the function be a no-op on a BIOS machine (because varstored won't be running, so won't have any dir)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The directory is created by the host installer when the varstored package is installed, should always be there.
If it is missing nothing will notice that these files are missing, and starting a UEFI VM would fail regardless of secureboot already, so if we want to check anything related to that then secureboot is not the place for it.

Lets drop this whole else try branch.

@rwdrich
Copy link
Contributor Author

rwdrich commented Feb 13, 2019

Don't merge

ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
@rwdrich rwdrich force-pushed the private/richarddav/CP-30434 branch 2 times, most recently from 9e5244d to def7d25 Compare February 21, 2019 15:23
ocaml/xapi/xapi_vm.ml Outdated Show resolved Hide resolved
ocaml/xapi/xapi_vm.ml Show resolved Hide resolved
with Sys_error _ ->
debug "varstored directory does not exist";
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The directory is created by the host installer when the varstored package is installed, should always be there.
If it is missing nothing will notice that these files are missing, and starting a UEFI VM would fail regardless of secureboot already, so if we want to check anything related to that then secureboot is not the place for it.

Lets drop this whole else try branch.

Copy link
Contributor

@edwintorok edwintorok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, might want to squash all the fixups

The first time a VM is booted transform `platform:secureboot=auto` to `true` if
`Pool.uefi_certificates` field is present and into `false` otherwise.

Signed-off-by: Richard Davies <richard.davies@citrix.com>
@rwdrich rwdrich merged commit 4f7220b into xapi-project:uefi Feb 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants