Compare commits

...

118 Commits

Author SHA1 Message Date
Steffen Eiden
f270ac99f0 New release s390-tools-2.33.0
Signed-off by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 17:53:57 +02:00
Marc Hartmayer
f83af8e076 rust: Generate shell (e.g. bash) completion scripts via build.rs for all tools
It might be handy to have shell completion support for the Rust PV
tools.

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:54:25 +02:00
Marc Hartmayer
4d2c92f6d5 rust/pvapconfig: Fix expected out doc comment
error[E0753]: expected outer doc comment

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:54:21 +02:00
Steffen Eiden
503e241db1 rust: Improve code formatting
Do some formatting that are in experimental stage but improve the code
readability.

Use rustfmt with a nightly toolchain and enable:

format_code_in_doc_comments = true
reorder_impl_items = true
comment_width = 100
wrap_comments = true
normalize_comments = true

(see .rustfmt.toml)

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:54:17 +02:00
Steffen Eiden
dea5f80215 libpv: Remove unused code
Remove all the code just pvattest-C used from libpv.
z(get)dump is the only user as of now.

Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:54:14 +02:00
Steffen Eiden
c261db259b zdump: Use constant for CCK size
This will reduce code dependency to otherwise unused code in libpv.
This code will be removed with the next patch.

Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:54:10 +02:00
Steffen Eiden
c382e7ef44 Remove pvattest-C and switch to pvattest-Rust implementation
Removes the C implementation of pvattest.
Use the Rust implementation instead.

Closes: https://github.com/ibm-s390-linux/s390-tools/issues/164
Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:54:06 +02:00
Steffen Eiden
16610a211f rust: pvattest-Rust
Add a CLI compatible Rust implementation of pvattest-C.
 - All (non-experimental) options are supported and work exactly as in
   the C implementation. For some options/parameters new variants are
   available.
 - `perform` now also accepts positional arguments, while keep accepting
   -i and -o  that was mandatory in the C implementation.
 - `version` may also be a command instead of an option now.
 - -V is deprecated
 - -v increases verbosity instead of showing the version
 - all experimental options are dropped

Acked-by: Qi Feng Huo <huoqif@cn.ibm.com>
Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:54:01 +02:00
Steffen Eiden
4d57ff046d rust: Prepare Cargo.toml for crates.io
Renames pv crate to s390_pv and pv_core to s390_pv_core. pv was already
taken on crates.io.

Bump the versions of all crates to 0.10.0. From now on we follow Semver
compatibility rules when it comes to updates. patch-level updates will
not introduce any backwards incompatible changes. For now all crates in
this directory will have the same version number. A version update may,
therefore, not add any new things.

Library users in this repository still use the non prefixed names and
rename the crate in the Cargo.toml. Doc-tests have to use the new name
however.

Add some Cargo metadata to the Cargo.toml.

Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:57 +02:00
Marc Hartmayer
4a76efe6d5 rust: Use AsRef<Path> and PathBuf in libraries
Use `AsRef<Path>` instead of `&Path`, &str, .... to be more versatile
and accept more input types. In addition, use `PathBuf` and `Path` for
paths instead of `String` and `str`.

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:47 +02:00
Steffen Eiden
87d43c7a32 rust/pv_*: Add more deny lints to pv and pv_core
Denies compiling if one of the following lints find something in pv or
pv_core:
    missing_docs,
    missing_debug_implementations,
    trivial_numeric_casts,
    unstable_features,
    unused_import_braces,
    unused_qualifications

Those lint force developers to avoid unnecessary code and providing
debuggability & documentation for each public symbol.

Fix the compile time error introduced with those lints.

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:43 +02:00
Steffen Eiden
f383278a5a rust/pv: Fix styling issues
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:39 +02:00
Steffen Eiden
a9d4b1e1b9 rust/pv: Inline openssl_extensions sub-crate
crates.io does not like sub-crates in a crate. Unpack the
openssl-extensions sub-crate into a (private) module.

While at it, fix some styling issues.

Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:35 +02:00
Steffen Eiden
61c5d7d431 rust/pv: Attestation generation and verification support
Add functionality to generate Attestation Measurement requests.
Add functionality to verify Attestation Measurement responses.

Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:32 +02:00
Steffen Eiden
b7765993e2 rust/pv_core: Attestation support
Add functionality for:

* sending attestation requests to the uvdevice and retrieve the
  response
* create/read the attestation exchange format file format

Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:27 +02:00
Steffen Eiden
e152b554f5 rust/pv: Add decrypt and HMAC function
Add a function to decrypt a block with aes_gcm.
Add functionality to perform HMAC operations.

Acked-by: Qi Feng Huo <huoqif@cn.ibm.com>
Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:21 +02:00
Steffen Eiden
c46a066827 rust/utils: Add Hexslice
Add a thin wrapper around [u8] to be able to represent an u8-slice as a
hex-string for Display and Serialize.

Acked-by: Qi Feng Huo <huoqif@cn.ibm.com>
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:12 +02:00
Steffen Eiden
cf003379ac rust/pv_core: Remove !#[allow(unused)]
Remove !#[allow(unused)] in pv_core and fix all unused warnings from the
Rust compiler.

Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:07 +02:00
Steffen Eiden
7b94783cb7 rust/pv: More documentation
Improve the API documentation of the pv crate.

Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:53:03 +02:00
Steffen Eiden
38600bb4e2 rust/pv: Add conversion implementations
Add AsRef<[u8]> and TryFrom<Vec<u8>> implementations so that the tag
data can be converted into base64.
See: https://docs.rs/serde_with/latest/serde_with/base64/struct.Base64.html

While at it, fix some typos and simplify the tag extraction.

Suggested-by:  Qi Feng Huo <huoqif@cn.ibm.com>
Acked-by: Qi Feng Huo <huoqif@cn.ibm.com>
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:56 +02:00
Marc Hartmayer
16875f7c6d rust/pv: Fix clippy finding
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:51 +02:00
Steffen Eiden
381fecfc44 rust: Refactoring and reduce API surface
Prepare pv & pv_core crates to be released on crates.io:
* Remove any unused API to stay flexible
* Remove utils dependency
* Move cli, tmpfile and version utilities to local utils crate
* Use the new utilities in the pv tools
* Rename Secret into Confidential to avoid confusion of Secret (now
  Confidential) and AddSecret requests.
* Move the uvsecret module out of the request module and change the name
  to secret.
* Cleanup dependencies
* Precise and correct minimal dependency versions
* Inline `Aes256Key::from_digest`

The cleanup ensures that the code also compiles with the dependencies
resolved to their minimal versions using:

$ cargo +nightly -Z minimal-versions update
$ cargo build

For more information refer to this blog post:
https://users.rust-lang.org/t/psa-please-specify-precise-dependency-versions-in-cargo-toml/71277/8

Signed-off-by: Marc Hartmayer <mhartmay@de.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:42 +02:00
Marc Hartmayer
5648b924d6 rust/pvsecret: verify.rs: fix clippy finding
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:38 +02:00
Steffen Eiden
636d2d571b rust/pvsecret: Update man files and help
Add subcommands in the man description. Add description for the help option.
Fix some minor wording issues.
Add the curve type  in the --user-data option of create.

Acked-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:35 +02:00
Marc Hartmayer
d016ec129d pre-commit-config: exclude .key and .bin files
Modifying binaries or key material using pre-commit makes no sense.

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:31 +02:00
Peter Oberparleiter
cb77faeae7 chpstat: add tool to display channel-path statistics
Add a new tool named chpstat that can be used to view channel-path
statistics such as utilization and I/O throughput, and to query and
control the status of the channel-path statistics function.

Note: Channel-path statistics are only available on systems running in
      an LPAR or DPM partition.

When run without further options, data for all channel-paths is
displayed repeatedly with a 5 second delay in table format.

Example output:

      CHANNEL-PATH       UTILIZATION(%)   READ(B/s)  WRITE(B/s)
  ID TYP CMG SHR SPEED  PART TOTAL  BUS  PART TOTAL  PART TOTAL
  1d  25   2   1     -  7.16  7.50 7.50  129M  129M  0.00  161K
  21  1b   2   1   32G  0.00  0.00 0.00  0.00  0.00  0.00  0.00
  34  1b   2   1   32G  0.00  0.00 0.00  0.00  0.00  0.00  0.00
  61  25   2   1     -  0.00  0.01 0.00  0.00 2.00K  0.00  307K
  63  25   2   1     -  0.00  0.01 0.00  0.00  0.00  0.00  381K
  bd  11   2   1   10G     -     -    - 529.8 532.1 616.3 616.3

Reviewed-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:27 +02:00
Peter Oberparleiter
38ea8fc3ee libutil: add output format helpers
Add helper functions for converting structured key-value data into
different formats such as JSON, text pairs, and CSV.

Using these functions the resulting output format can be dynamically
configured at run-time without the need to duplicate output-generating
code for each format type. Also format-specific requirements such as
quoting, indentation, and comma-placement are automatically taken care
of.

Basic API calling sequence:

 util_fmt_init()      => Select output format
 util_fmt_obj_start() => Start a new object or list
 util_fmt_pair()      => Emit a key-value pair
 util_fmt_obj_end()   => End the most recent object or list
 util_fmt_exit()      => Cleanup

Notes:
 - Supported data elements are objects, lists and key-value pairs
   (mappings)
 - Scalars are only supported as part of a mapping

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:23 +02:00
Peter Oberparleiter
d8e5bc07aa libutil: add function to concatenate a format string in place
Add function util_concatf() that appends the result of a format string
expansion to the end of an existing string while taking care of the
required memory allocations.

Usage example:

  char *str = NULL;

  util_concatf(&str, "list:");
  for (int i = 1; i <= 3; i++)
    util_concatf(&str, "%spart%d", (i > 1 ? "," : ""), i);
  printf("%s\n", str); /* list:part1,part2,part3 */

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:20 +02:00
Peter Oberparleiter
3b26f79143 libutil: add dynamic array helpers
Add helper macros to easily create, enlarge and append new elements to
dynamic arrays of arbitrary types.

Note: The use of dynamic arrays over lists may be preferable in some
cases to reduce complexity, and they may be required in cases where
elements need to be addressed directly by index.

Usage example:

  struct {
    int a;
    int b;
  } *array = NULL, element = { 1, 2 };
  unsigned int num = 0;

  util_add_array(&array, &num, element);
  printf("array[0].a=%d\n", array[0].a); /* array[0].a=1 */
  printf("array[0].b=%d\n", array[0].b); /* array[0].b=2 */

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:16 +02:00
Peter Oberparleiter
c366429e57 libutil: fix hexdump indentation
The current implementation of util_hexdump_grp() enforces a minimum
indentation of 1 space which may not be suitable for all users.

Fix this by allowing a true zero indentation level.

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:13 +02:00
Peter Oberparleiter
8ed478ce46 libutil: enable record output without separator
The separator line emitted by util_rec functions may not be suitable for
all users. Fix this by making the hdr_sep parameter optional.

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:09 +02:00
Peter Oberparleiter
a97a8f1cba libutil: fix util_file_write_* return code
By default, files opened via fopen are block-buffered. As a result,
I/O errors that occur during file write operations via util_file_write_*
are silently ignored because fputs() only buffers data while actual I/O
occurs during the flush operation that is part of the final fclose()
library call.

Fix this by indicating errors that occur during fclose() via the
util_file_write_* function return code.

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:07 +02:00
Eduard Shishkin
fb0b6263d1 zipl/src: Fix problems when target parameters are specified by user
Steps to reproduce: Prepare some target disk for IPL, specifying
its parameters via zipl "target options", and an image IMAGE_NAME
located on another disk. Don't specify "-a" option.

Actual result: Installation succeeded (resulting in unbootable setup)
Expected result: "Error: Could not add image file 'IMAGE_NAME': File
is not on target device"

The problem is in incorrect evaluation of device number(dev_t) where
the image is located by the function add_component_file_range() in
case when target parameters are specified by user.

Fixup: Retrieve info of the underlying disk without any user hints,
passing zeroed structure job_target_data

Signed-off-by: Eduard Shishkin <edward6@linux.ibm.com>
Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:52:03 +02:00
Eduard Shishkin
76aaf3d4a8 zipl/src: Fix problems when image is not on target SCSI
This patch fixes a bug in disk_get_info()
Steps to reproduce: Prepare a SCSI disk for IPL, specifying an image
("-i IMAGE_NAME") located on DASD and a target directory ("-t /mnt")
located on SCSI (dm). Don't specify "-a" option.

Actual result: Installation succeeded (resulting in unbootable setup!)
Expected result: "Error: Could not add image file 'IMAGE_NAME':
File is not on target device".

The problem is in incorrect evaluation of device number (dev_t) of
the device, where the image file is located, by the function
add_component_file_range(). To evaluate it, disk_get_info() is called
with the structure job_target_data (passed as the second argument)
previously completed by disk_get_info() called earlier to evaluate
parameters of the specified target device (SCSI dm) by the function
prepare_build_program_table_file(). Since the targetbase is already
set in the passed job_target_data (by the first call), in the second
call the source type is evaluated as "source_user", so the number of
the device where the image is located is calculated by the base SCSI
disk, which is incorrect.

Fixup: Rework disk_get_info(): introduce a dedicated function to
evaluate source type not depending on the job_target_data content.
Implement the core procedure as a switch by the evaluated source
type.

Signed-off-by: Eduard Shishkin <edward6@linux.ibm.com>
Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:59 +02:00
Eduard Shishkin
08232d29b9 zipl/src: Drop "bootmap_dir" field of struct install_info
Use "bootmap_dir" field of struct job_target_data instead,
thus avoid allocation/releasing additional resources.

Signed-off-by: Eduard Shishkin <edward6@linux.ibm.com>
Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:56 +02:00
Eduard Shishkin
cfaccc5fad zipl/src: Fix problems when image is not on target DASD
This patch fixes a bug in disk_get_info()
Steps to reproduce: Prepare a DASD disk for IPL, specifying an
image ("-i IMAGE_NAME") located on SCSI (dm) and a target directory
("-t /mnt") located on DASD. Don't specify "-a" option.

Actual result: "Run /lib/s390-tools//zipl_helper.device-mapper /mnt
Error: Could not retrieve device-mapper information for device
'dasda1'"
Expected result: "Run /lib/s390-tools//zipl_helper.device-mapper 253:4
Error: Could not add image file 'IMAGE_NAME': File is not on target
device"

The problem is in incorrect calculation by disk_get_info()
parameters for @device associated with the image file. Specifically,
@target->bootmap_dir is passed to the script, which is wrong.

Fixup: Get rid of bogus branching in disk_get_info() in case when
target parameters are evaluated in "source_script" mode. Always pass
major and minor of the @device (whose parameters to be calculated)
to the helper script.

Signed-off-by: Eduard Shishkin <edward6@linux.ibm.com>
Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:52 +02:00
Jan Höppner
7da5a6b9ed tunedasd: Fix missing hyphen escapes in the man page
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:48 +02:00
Jan Höppner
7ed87bed8c tunedasd: Fix trailing whitespace
Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:45 +02:00
Jan Höppner
46ec94d0ce zdsfs: Fix missing hyphen escapes in the man page
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:42 +02:00
Jan Höppner
6bc0b023aa dasdstat: Fix missing hyphen escapes in the man page
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:38 +02:00
Jan Höppner
13d673e0f7 dasdstat: Fix man page title
The man page title was incorrectly set to "LSDASD". Set the correct name
"DASDSTAT".

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:35 +02:00
Jan Höppner
c40c159173 lsdasd: Fix missing hyphen escapes in the man page
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:32 +02:00
Jan Höppner
202ded5d11 tape390: Fix missing hyphen escapes in the man pages
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:28 +02:00
Jan Höppner
e3a698c382 tape390: Fix trailing whitespace in man pages
Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:22 +02:00
Jan Höppner
3a60b4caa8 fdasd: Fix missing hyphen escapes in the man page
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:19 +02:00
Jan Höppner
f57858b3a8 dasdview: Fix missing hyphen escapes in the man page
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped. Fix whitespace damage
along the way.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:15 +02:00
Jan Höppner
9e430b9010 dasdinfo: Fix missing hyphen escapes in the man page
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:10 +02:00
Jan Höppner
ad2a42ce9e dasdfmt: Fix missing hyphen escapes in the man page
Hyphens are converted by groff to a different unicode character leading
to failing command execution of copy-pasted options or examples.

Ensure that all hyphens are properly escaped.

Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:05 +02:00
Nikita Dubrovskii
1c32635b3a chreipl: Impove disk type detection when running under QEMU
Under QEMU user can attach disk with smth like:
```
  -device virtio-scsi-ccw,... -device scsi-hd,...
```
So virtio block device appears as '/dev/sda' instead of '/dev/vda'.
chreipl assumes all '/dev/sd*' disks as FCP disks, which is not a
case in such setup.

Closes: https://github.com/ibm-s390-linux/s390-tools/pull/154
Signed-off-by: Nikita Dubrovskii <nikita@linux.ibm.com>
Acked-by: Marc Hartmayer mhartmay@linux.ibm.com
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:51:00 +02:00
Nikita Dubrovskii
558594fddf ipl_tools/ccw: Introduce device_sysfs_path()
Add helper function to get device's real path under SYSFS_ROOT devices
hierarchy.

Github-ID: https://github.com/ibm-s390-linux/s390-tools/pull/154
Signed-off-by: Nikita Dubrovskii <nikita@linux.ibm.com>
[hoeppner@linux.ibm.com: Adapt commit message]
Acked-by: Marc Hartmayer mhartmay@linux.ibm.com
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:42 +02:00
Harald Freudenberger
c8879322b0 pvapconfig: Escape hyphens in man page correctly
Insert backslash(es) to escape the hyphens used
in the pvapconfig man page correctly.

Suggested-by: Jan Hoeppner <hoeppner@linux.ibm.com>
Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:27 +02:00
Harald Freudenberger
d716c553c4 chzcrypt: Escape hyphens in man page correctly
Insert backslash(es) to escape the hyphens used
in the chzcrypt man page correctly.

Suggested-by: Jan Hoeppner <hoeppner@linux.ibm.com>
Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:21 +02:00
Harald Freudenberger
ab35922161 lszcrypt: Escape hyphens in man page correctly
Insert backslash(es) to escape the hyphens used
in the lszcrypt man page correctly.

Suggested-by: Jan Hoeppner <hoeppner@linux.ibm.com>
Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:18 +02:00
Matthew Rosato
474c6adf8f libap: use custom wait time for file locks
If many invocations of mdevctl occur simultaneously (as can happen with
libvirt) then waiting for 5-60 seconds per lock retry is simply too long.
Anticipating this possibility, retry more frequently but also attempt
significantly more retries than before.

Reported-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Reviewed-by: Marc Hartmayer <marc@linux.ibm.com>
Tested-by: Marc Hartmayer <marc@linux.ibm.com>
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:15 +02:00
Matthew Rosato
f5f806af06 libutil/util_lockfile: allow for custom lock wait/retry time
The default values for repeated attempts at acquiring a file lock created
by util_lockfile are on the order of seconds.  Let's leave this the
default, but allow for a caller to specify smaller values by adding
cw (custom_wait) functions and by switching from using sleep to usleep.

Reviewed-by: Anthony Krowiak <akrowiak@linux.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:12 +02:00
Ingo Franzki
0a01719477 zcryptstats: Fix missing hyphen escapes in man pages
Ensure that all hyphens in command options and examples are escaped properly.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:08 +02:00
Ingo Franzki
07cd9143da zkey: Fix missing hyphen escapes in man pages
Ensure that all hyphens in command options and examples are escaped properly.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:04 +02:00
Mikhail Zaslonko
d064cb522f include/boot: Sync os_info.h with kernel version.
Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:50:00 +02:00
Alexander Egorenkov
802c7db50e zfcpdump: fix hsa release for V!=R kernel
Since V!=R kernel introduction, HSA memory can be
contained in/spread over more than one ELF LOAD segment of
/proc/vmcore simultaneously. Therefore, the old HSA release logic is no
longer valid because it assumes that HSA memory is fully contained in
exactly one ELF LOAD segment of /proc/vmcore. This resulted in zfcpdump
releasing HSA memory too soon and by that making parts of /proc/vmcore
which cover HSA memory unreadable by user space. To correct this
problem on V!=R kernel, we need first to find all ELF LOAD segments
containing HSA memory, write those /proc/vmcore parts first and only then
release HSA memory. The new HSA release logic must be able to handle
both V!=R and V==R kernels to be backwards compatible.

====================
Tests of V!=R kernel
====================

Output of test run (KASLR on)
=============================

Writing dump:

TRACE: Read: /sys/kernel/debug/zcore/hsa:
TRACE: '2ffff000'

TRACE: ELF LOAD segment: p_offset=0x0000000000009000 p_filesz=0x0000000001cb0000 p_paddr=0x00000002f1e30000 p_vaddr=0x000002c609044000
TRACE: ELF LOAD segment: p_offset=0x0000000001cb9000 p_filesz=0x0000000040000000 p_paddr=0x0000000000000000 p_vaddr=0x000001bd00000000
TRACE: ELF LOAD segment: p_offset=0x0000000041cb9000 p_filesz=0x0000000300000000 p_paddr=0x0000000100000000 p_vaddr=0x000001be00000000

TRACE: Write copy table entry 0: off=0x0000000001cb9000 size=0x000000002ffff000 hsa=1
TRACE: Write copy table entry 1: off=0x0000000000001000 size=0x0000000001cb8000 hsa=0
TRACE: Release HSA memory
TRACE: Write copy table entry 2: off=0x0000000031cb8000 size=0x0000000310001000 hsa=0
TRACE: Write copy table entry 3: off=0x0000000000000000 size=0x0000000000001000 hsa=0

Dump successful

Output of test run (KASLR off)
==============================

Writing dump:

TRACE: Read: /sys/kernel/debug/zcore/hsa:
TRACE: '2ffff000'

TRACE: ELF LOAD segment: p_offset=0x0000000000009000 p_filesz=0x0000000001cb0000 p_paddr=0x0000000000c21000 p_vaddr=0x000003ffe0000000
TRACE: ELF LOAD segment: p_offset=0x0000000001cb9000 p_filesz=0x0000000040000000 p_paddr=0x0000000000000000 p_vaddr=0x000002f200000000
TRACE: ELF LOAD segment: p_offset=0x0000000041cb9000 p_filesz=0x0000000300000000 p_paddr=0x0000000100000000 p_vaddr=0x000002f300000000

TRACE: Write copy table entry 0: off=0x0000000000009000 size=0x0000000001cb0000 hsa=1
TRACE: Write copy table entry 1: off=0x0000000001cb9000 size=0x000000002ffff000 hsa=1
TRACE: Write copy table entry 2: off=0x0000000000001000 size=0x0000000000008000 hsa=0
TRACE: Release HSA memory
TRACE: Write copy table entry 3: off=0x0000000031cb8000 size=0x0000000310001000 hsa=0
TRACE: Write copy table entry 4: off=0x0000000000000000 size=0x0000000000001000 hsa=0

Dump successful

====================
Tests of V==R kernel
====================

Output of test run (KASLR on)
=============================

Writing dump:

TRACE: Read: /sys/kernel/debug/zcore/hsa:
TRACE: '2ffff000'

TRACE: ELF LOAD segment: p_offset=0x0000000000009000 p_filesz=0x0000000000000000 p_paddr=0x0000000000000000 p_vaddr=0x0000000000000000
TRACE: ELF LOAD segment: p_offset=0x0000000000009000 p_filesz=0x0000000040000000 p_paddr=0x0000000000000000 p_vaddr=0x0000000000000000
TRACE: ELF LOAD segment: p_offset=0x0000000040009000 p_filesz=0x0000000300000000 p_paddr=0x0000000100000000 p_vaddr=0x0000000100000000

TRACE: Write copy table entry 0: off=0x0000000000009000 size=0x000000002ffff000 hsa=1
TRACE: Write copy table entry 1: off=0x0000000000001000 size=0x0000000000008000 hsa=0
TRACE: Release HSA memory
TRACE: Write copy table entry 2: off=0x0000000030008000 size=0x0000000310001000 hsa=0
TRACE: Write copy table entry 3: off=0x0000000000000000 size=0x0000000000001000 hsa=0

Dump successful

Output of test run (KASLR off)
==============================

Writing dump:

TRACE: Read: /sys/kernel/debug/zcore/hsa:
TRACE: '2ffff000'

TRACE: ELF LOAD segment: p_offset=0x0000000000009000 p_filesz=0x0000000000000000 p_paddr=0x0000000000000000 p_vaddr=0x0000000000000000
TRACE: ELF LOAD segment: p_offset=0x0000000000009000 p_filesz=0x0000000040000000 p_paddr=0x0000000000000000 p_vaddr=0x0000000000000000
TRACE: ELF LOAD segment: p_offset=0x0000000040009000 p_filesz=0x0000000300000000 p_paddr=0x0000000100000000 p_vaddr=0x0000000100000000

TRACE: Write copy table entry 0: off=0x0000000000009000 size=0x000000002ffff000 hsa=1
TRACE: Write copy table entry 1: off=0x0000000000001000 size=0x0000000000008000 hsa=0
TRACE: Release HSA memory
TRACE: Write copy table entry 2: off=0x0000000030008000 size=0x0000000310001000 hsa=0
TRACE: Write copy table entry 3: off=0x0000000000000000 size=0x0000000000001000 hsa=0

Dump successful

Signed-off-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Reviewed-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:49:54 +02:00
Marc Hartmayer
0f87acc2b3 common.mak: Improve overall Makefile performance
Improve overall Makefile performance by dramatically reducing the number
of fork and exec system calls through the use of simply expanded
variables [1]. Before this change, a simple `make -C libutil` invocation
used over 4000 execs, after this change it was reduced to just over 300
execs.

[1] https://www.gnu.org/software/make/manual/html_node/Setting.html

Reported-by: Ingo Franzki <ifranzki@linux.ibm.com>
Tested-by: Ingo Franzki <ifranzki@linux.ibm.com>
Reviewed-by: Ingo Franzki <ifranzki@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-05-27 16:49:05 +02:00
Eduard Shishkin
998b61e5f3 dasdfmt: Change mode default
When formatting an ESE (thin-provisioned) ECKD DASD, dasdfmt(8)
defaults to the quick-format mode instead of full-format for normal
DASDs. This results in a significant performance impact during first
sequential write to each track, which may be unexpected for users.

To address this, change the default for dasdfmt to always use
full-format mode. Customers that require thin provisioning(*) still
override the default by specifying quick format explicitly using the
"-M" option.

Get rid of the related fallbacks; In case of unsuccessful space
release always fail. The customers that still require quick format can
proceed by specifying "--no-discard" option.

(*) Thin provisioning: while providing a large amount of logical
    space, zero amount of actual space is provisioned and then
    allocated on an on-demand basis.

Signed-off-by: Eduard Shishkin <edward6@linux.ibm.com>
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Joern Siglen
c47071815b dbginfo.sh: use POSIX uname options
uname -i is not defined POSIX and will fail on some linux distributions

Reviewed-by: Michael Storzer <mstorzer@de.ibm.com>
Signed-off-by: Joern Siglen <siglen@de.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Mikhail Zaslonko
0ed6c1ccde zfcpdump: Search for zero paddr vmcore load to identify HSA
Check for vmcore LOAD segment with zero paddr (instead of zero vaddr) to
identify HSA since physical and virtual addresses can be uncoupled on s390.

Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Acked-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Vineeth Vijayan
d888a27f07 zconf/chp: add reference to zos document
The 'lschp' command's output contains a 'type' identifier column. For
information about each value of this identifier, provide the
reference to the z/OS public documentation in the manpage.

Suggested-by: Mike Storzer <MSTORZER@de.ibm.com>
Signed-off-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
7c2ae3d2e8 rust/pvsecret: Streamline arch dependend code
Get rid of all arch barriers in main.rs. cmd.rs handles the arch
barriers for the individual commands. Simplifies main.rs & cmd.rs and
makes it easier to read and understand the code.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
c88a8b6130 rust/pvapconfig: Fix Cargo clippy findings
Fix findings from `cargo clippy --all-targets`. `warning: calls to
`push` immediately after creation` The findings were in test code only.

Also, replace a while loop with a function from Vec.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
32a0434bc3 rust/pv_core: Always report rc and rrc for an UV error
The (non-archiected) rrc helps to debug issues. Therefore report them
also in release builds.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
a2e556858a rust/pv_core: Remove unnecessary mirrored constants
They add no value and code outside the crate does not need those constants.
Reduces unnecessary constant duplication. Introduce an error for to
large Add-Secret requests and check for this to render those contsnts
fully unnecessary for the API.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
9eab43994b rust/pv: Remove internal BinGuestSecret struct
The BinGuestSecret type provides no benefits. The public GuestSecret
struct can handle everything. Therefore, move the two functions from bin
to the non-bin variant. While at it, use a struct to define the binary
structure instead of copy numbers to some positions in a Vec. This
simplifies the addition of further secret types.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
c53dfa9754 rust/pv: Use a SecretId struct instead of an array
This streamlines and unifies the use and (de)serialization of structs
using a secret id. As a bonus, the hidden `for_pv` module is not longer
needed.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
81a1e13f3b rust/pv: Provide default for UvCmd::cmd
Provides a default for the cmd function for an UvCmd. This is enabled by
requiring an associated constant for the IOCTL nr of the command.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
8f89234f1a rust/pv: Remove dangling file
The content of `pv/src/uvsecret/uvc.rs` was moved with
9b51b8b882 ("rust/pv: Refactor pv crate") to pv_core.
The content was unused, but the file was not deleted.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
d757dbfbff rust/pv: Introduce AesGcmResult type
Fix clippy waring `warning: very complex type used.` by introducing a
new struct containing the tuple, that was returned before.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
0d9a6e45fb rust/pv_core: Refractor secret list to own file
This makes the secret.rs file more readable.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
4d4666cff7 rust/README.md: Add pvapconfig to the tools section
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Julian Ruess <julianr@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Steffen Eiden
46092add29 rust/pv_core: Fix lazy-static dev-dependency
During the remove of mockito, lazy_static dependency was accidentally
removed as well.
Fix this by adding lazy-static as dev dependency again.

Fixes: aba8900074 ("rust/pv_core: Remove mockito dependency")
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 15:07:19 +02:00
Finn Callies
1655c39ded cpacfstats: add counter numbers to output
This adds the counter numbers to the corresponding counter names to the
cpacfstats output. This aims to ease using this tool with other related
tools which may use other names for the counters.

Signed-off-by: Finn Callies <fcallies@linux.ibm.com>
Reviewed-by: Harald Freudenberger <freude@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 14:37:52 +02:00
Vineeth Vijayan
a95dc09c6e zdev: add common header for chzdev generated tempfile too
All files created by chzdev feature a common header; however, this
header is absent in temporary files. It is necessary to incorporate
the consistent "Generated by chzdev" header into temporary files
generated by chzdev as well, so as to properly identify these files
via option --is-owner.

Signed-off-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 14:37:52 +02:00
Vineeth Vijayan
f1f80a8a20 zdev: implement option to identify files created by zdev
Enhance the 'chzdev' tool by introducing a new option to discern
files created by 'zdev-tools.' The command usage is as follows:

$ chzdev --is-owner <file-name>

When executed, the command will return an exit code of 0 for all
files generated by zdev-tools. In the case of an unknown file, the
tool will return the exit code 'EXIT_UNKNOWN_FILE i.e 33.'

Signed-off-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 14:37:52 +02:00
Vineeth Vijayan
4c2bfb1d47 zdev: rename site.h to zdev.h
Add zdev-specific definitions into a lightweight header file. Rather
than creating a new one, transform the existing "site.h" into "zdev.h"
to house all generic zdev-specific definitions that needs to be shared
between chzdev, lszdev and zdev_id going forward.

Signed-off-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 14:37:51 +02:00
Finn Callies
c0fc21efb3 cpacfstats: dynamically load PAI and CPUMF counter
Up until now cpacfstats assumes that on the running hardware either all
PAI and CPUMF are available or non at all.  Which counters are supported
may be hardware dependent and can vary in the future. With
this commit cpacfstats dynamically loads the counters from sysfs entries
in (/sys/devices/pai_crypto/events/) and (/sys/devices/cpum_cf/events/)
respectively.

Additionally cpacfstats has a new way of determining which PAI counters
are meant for kernel usage.

Signed-off-by: Finn Callies <fcallies@linux.ibm.com>
Reviewed-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Ingo Franzki <ifranzki@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-04-26 14:37:51 +02:00
Steffen Eiden
93d3c44a1a Prepare for next release
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-04-03 16:18:09 +02:00
Steffen Eiden
9eea78b3ad New release s390-tools-2.32.0
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-04-03 16:18:09 +02:00
Steffen Eiden
0a3a556879 rust/Cargo.lock: Update curl-sys crate
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-04-03 16:18:09 +02:00
Steffen Eiden
aba8900074 rust/pv_core: Remove mockito dependency
With f6c6f0cc71 ("rust/pv/test: Code + Certificate refactoring")
no code uses mockito anymore, but it's dependency was not removed.

Remove the mockito dependency from the rust workspace.

Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-04-03 16:18:09 +02:00
Dan Horák
f5744b95db genprotimg: Fix build with OpenSSL 1.1
OpenSSL 1.1 seems to use a non-const parameter to X509_name_dup(), but
x509_armonk_locality_fixup() is passing a const there. The compile then
fails on "discards 'const' qualifier", when -Werror is used. Thus
resolve with a type-cast like in c2b_name().

Fixes: https://github.com/ibm-s390-linux/s390-tools/pull/167
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Dan Horák <dan@danny.cz>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-04-02 17:37:53 +02:00
Dan Horák
13d721afd3 libpv: Fix build with OpenSSL 1.1
OpenSSL 1.1 seems to use a non-const parameter to X509_name_dup(), but
x509_armonk_locality_fixup() is passing a const there. The compile then
fails on "discards 'const' qualifier", when -Werror is used. Thus
resolve with a type-cast like in pv_c2b_name().

GitHub-ID: https://github.com/ibm-s390-linux/s390-tools/pull/167
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Dan Horák <dan@danny.cz>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-04-02 17:37:53 +02:00
Eduard Shishkin
90a2e6d70e zipl/src: Fix leak of files
Fix leak of temporary files: if prepare_build_program_table_file()
is called in no dry-run mode and there was an error then the file
@filename will not be deleted in free_bootloader()).

Fix leak of renamed files as well as corruption of previously
created bootmap files with the same name in case of unsuccessful
IPL installation.

Add a special flag to keep a track of file's "temporary" state;
Base the cleanup decision on this flag instead of checking dry-run;
Release resources captured by prepare_bootloader() in the error path;
Move the final rename to be called only after successful installation.

                    Original logic:

prepare_bootloader_ipl():
prepare_bootloader_ngdump():

  always create temporary @filename
  if (!dry-run) rename @filename;
  install;
  cleanup: if (dry_run) drop @filename (*** LEAK ***)

prepare_bootloader_device():

  if (dry_run) create temporary @filename
  install, don't rename;
  cleanup: if (dry_run) drop @filename

                    New logic:

prepare_bootloader_ipl():
prepare_bootloader_ngdump():

  always create temporary @filename and set @tmp_filename_created;
  install;
  if (!dry_run) rename @filename and clear @tmp_filename_created;
  cleanup: if (@tmp_filename_created is set), drop @filename

prepare_bootloader_device():

  if (dry_run) create @filename and set @tmp_filename_created;
  install, don't rename;
  cleanup: if (@tmp_filename_created is set) drop @filename.

Fixes: https://github.com/ibm-s390-tools/s390-tools/issues/165
Signed-off-by: Eduard Shishkin <edward6@linux.ibm.com>
Reviewed-by: Stefan Haberland <sth@linux.ibm.com>
Acked-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-04-02 17:37:53 +02:00
Marc Hartmayer
bc9f8a8100 genprotimg: Fix out-of-disk space handling
Convert the assertion to a GError since an out-of-disk-space situation
is a valid situation that should be handled.

ERROR:utils/crypto.c:1843:__encrypt_decrypt_bio: assertion failed: (num_bytes_written == out_len)
Bail out! ERROR:utils/crypto.c:1843:__encrypt_decrypt_bio: assertion failed: (num_bytes_written == out_len)

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-04-02 17:37:53 +02:00
Steffen Eiden
2b5e7b0491 pvattest: Fix root-ca parsing
The parser setup falsely set the argument type as filename array, but
code expected a single filename. Fixed by setting up the parser
correctly to expect a single file name.

Fixes: 3ab06d77fb ("pvattest: Create, perform, and verify attestation measurements")
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Steffen Eiden
d7c95265cd libpv: Support Armonk in IBM signing key subject
New IBM signing keys will have Armonk as locality in the subject.
Ensure that CRLs with Poughkeepsie as issuer locality are still
discovered if they are signed with the signing keys private key.
Also, drop the check for issuer/subject comparison and only rely on
validity period and cryptographic signatures.

Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Marc Hartmayer
8751cfc409 genprotimg/samples/check_hostkeydoc: code formatting
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Marc Hartmayer
01f96d30f6 genprotimg/samples/check_hostkeydoc: fix ShellCheck findings
Fix ShellCheck findings and two typos. White spaces in filenames are now
supported properly.

Reviewed-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Marc Hartmayer
fffbd93f12 genprotimg/samples/check_hostkeydoc: improve argument parsing
+ use `getopts` POSIX builtin instead of external program
+ improve error reporting
+ support white spaces in the script name

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Marc Hartmayer
ddcfbdc8d2 genprotimg/samples/check_hostkeydoc: support Armonk in IBM signing key subject
Newer IBM signing keys use `Armonk` in their subject therefore add
support for it.

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Marc Hartmayer
173fd7cdca genprotimg/samples/check_hostkeydoc: set LC_ALL=C for the sort command
Set `LC_ALL=C` for the `sort` command to get a stable sort order. See
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html for
details on the effect of LC_* on `sort`. Adapt the default issuer
accordingly.

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Marc Hartmayer
d14e7593cc genprotimg: support Armonk in IBM signing key subject
New IBM signing certificates will have 'Armonk' as locality in the
subject. Make sure that certificate revocations lists (CRL) with
'Poughkeepsie' as issuer locality are still considered as valid as long
as they are signed with the IBM signing keys private key. In addition,
drop the check for 'issuer(HKD) == subject(HKSK)' as it doesn't improve
security. While at it, remove now unused functions and fix a memory leak
of @akid in `check_crl_issuer`.

Reviewed-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Steffen Eiden
1a3d0b74f7 rust/pv: Support Armonk in IBM signing key subject
New IBM signing keys will have Armonk as locality in the subject.
Ensure that CRLs with Poughkeepsie as issuer locality are still
discovered if they are signed with the signing keys private key.
Also, drop the check for issuer/subject comparison and only rely on
validity period and cryptographic signatures.

Reviewed-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Marc Hartmayer
f6c6f0cc71 rust/pv/test: Code + Certificate refactoring
* Get rid of Mockito
* create certs with AKID
* simplify things in the `create_certs.py` script

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Thomas Richter
966e67a252 cpumf/lscpumf: add support for machine type 3932
Add support for machine type 3932 and list the
CPU Measurement facility counter sets.

Signed-off-by: Thomas Richter <tmricht@linux.ibm.com>
Acked-by: Sumanth Korikkar <sumanthk@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Joern Siglen
1c128c0d11 dbginfo.sh: dash compatible copy sequence
rewrite the copy of dbginfo.sh for dash compatibility

Reviewed-by: Mike Storzer <MSTORZER@de.ibm.com>
Reviewed-by: Mario Held <mario.held@de.ibm.com>
Signed-off-by: Joern Siglen <siglen@de.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
VasiliyS
a3199d58db rust/pv_core: Fix UvDeviceInfo::get() method.
`ATTESTATION_NR` flag was not set properly in case the device
didn't support `Info` IOCTL call.

Closes: https://github.com/ibm-s390-linux/s390-tools/pull/163
Signed-off-by: Vasiliy Suvorov <vsuvorov@gmail.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-22 12:07:29 +01:00
Vineeth Vijayan
659483031e zdev: Rearrange the options alphabetically in man page
re-arrange options in zdev manpages in the alphabetical order.

Signed-off-by: Vineeth Vijayan <vneethv@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:43 +01:00
Ingo Franzki
7dc2513205 zkey: Detect FIPS mode and generate PBKDF for luksFormat according to it
For LUKS2 volumes, zkey generates 'cryptsetup luksFormat' commands with
options '--pbkdf argon2i --pbkdf-memory 32 --pbkdf-force-iterations 4'
for low memory and time requirements. Using the default Argon2i options
might cause out-of-memory errors when multiple encrypted volumes are
unlocked automatically at boot through /etc/crypttab.

When the system runs in FIPS mode, which is indicated by file
/proc/sys/crypto/fips_enabled containing '1', the Argon2i password based
key derivation function might be disabled by a policy, and such
'cryptsetup luksFormat' commands might fail.

Generate '--pbkdf pbkdf2' instead if the system runs in FIPS mode.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:43 +01:00
Marc Hartmayer
0748d365a6 genprotimg/**/Makefile: Fix staged installs
Fix the support for staged installs. The Makefile variable `PKGDATADIR`
uses `DESTDIR` for all Makefile target, but actually it should only be
used for the `install*` and `uninstall*` targets. [1] Fix this by using
`DESTDIR` only for `install*` targets - uninstall* targets are not
supported by s390-tools.

Before this change, if `DESTDIR` was set for staged installs,
`genprotimg` has tried to find the bootloader binaries at the temporary
installation path `$DESTDIR$(TOOLS_DATADIR)/genprotimg/` instead of
`$(TOOLS_DATADIR)/genprotimg`.

[1] https://www.gnu.org/prep/standards/html_node/DESTDIR.html

Fixes: 65b9fc442c ("genprotimg: introduce new tool for the creation of PV images")
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:38 +01:00
Marc Hartmayer
94a404ed10 pvattest: Makefile: Remove unused variable and CFLAGS
Remove unused variables and unused compiler flags.

Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Thomas Richter
ef1799f31f s390-tools/libutil: Add machine type 3932
Add support for machine type 3932.
Print identical product name for machine types 8561 and 8562.

Signed-off-by: Thomas Richter <tmricht@linux.ibm.com>
Suggested-by: Christian Borntraeger <borntraeger@linux.ibm.com>
Acked-by: Christian Borntraeger <borntraeger@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Mikhail Zaslonko
47b0960cc7 zdump: Make ngdump_get_part_path() public
Make ngdump_get_part_path() public in order for unit-tests to access it.

Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Mikhail Zaslonko
2288331a6f zipl: Clear CCW-type DASD dumper upon ldipl-dump tool installation
Clear CCW-type DASD standalone dumper (if installed on the DASD) upon
successful installation of the List-Directed ECKD dump tool on the same
device. This helps to avoid 'zgetdump -d' confusion and have only one
'active' dump tool per disk.

Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Acked-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Mikhail Zaslonko
23e9156f43 s390_dump.h: Add DF_S390_DUMPER_MAGIC_SIZE constant
Add DF_S390_DUMPER_MAGIC_SIZE constant to s390_dump.h.
Use it instead of hardcoded length when processing dumper magics
in zgetdump code.

Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Acked-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Mikhail Zaslonko
8f99e7c4ea zdump: Move DUMPER_MAGIC constants to include/dump/s390_dump.h
Move DUMPER_MAGIC constants from df_s390.h to the global header
include/dump/s390_dump.h in order to reuse it in zipl code.

Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Reviewed-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Mikhail Zaslonko
f3bcd94524 zdump: Add zgetdump -d support for ECKD ldipl-dump
Make 'zgetdump -d' to identify ldipl-dump tool (ngdump) installed on DASD
volume just like it does for NVMe ngdump.

Output sample:
-------------
Dump device info:
  Dump tool.........: Next Generation (NGDump) dump tool
  Version...........: 1
  Architecture......: s390x (64 bit)

Partition info:
  Partition number..: 2

Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Reviewed-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Mikhail Zaslonko
a2f8b19c2a dumpconf: Update the man page and etc/sysconfig/dumpconf template
Update dumpconf man page with list-directed ECKD dump information (new
attributes and configuration example for ECKD LDIPL dump).
Update etc/sysconfig/dumpconf with list-directed ECKD dump configuration
example.

Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Reviewed-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Mikhail Zaslonko
588d720517 dumpconf: Process br_chr and bootprog eckd attributes
- Write the following sysfs attributes for list-directed ECKD devices
  introducing setup_eckd_device() function.
    br_chr: location of boot record
    bootprog: boot program selector
- Remove redundant parameters from setup_ccw_device() and setup_fcp_device()
  functioins.

Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Reviewed-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Ingo Franzki
cd822cb770 zkey: Fix convert command to accept only keys of type CCA-AESDATA
Reject conversion of other key types with a proper error message.
Also fix a typo in another error message of the convert command.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Ingo Franzki
b27b8e3cd3 zkey: Fix typos in error message
Use a colon instead of a semicolon in the message.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:34 +01:00
Steffen Maier
7a2c5dc980 zdev/dracut: Fix file mode of non-executable shell library zdev-lib.sh
Use similar `install` mode option in Makefile as in commit
9b2fb1d4d2 ("zdev: add helper to convert from dasd_mod.dasd to zdev
config").

Fixes: 73c46a3056 ("zdev/dracut: fix kdump by only activating required devices")
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Maier <maier@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
2024-03-15 15:11:28 +01:00
Jan Höppner
d9e3763d1c Prepare for next release
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
2024-02-02 17:49:44 +01:00
312 changed files with 16386 additions and 11566 deletions

1
.gitignore vendored
View File

@@ -92,6 +92,7 @@ vmcp/vmcp
vmur/vmur
zconf/chp/chchp
zconf/chp/lschp
zconf/chp/chpstat/chpstat
zconf/css/lscss
zconf/qeth/lsqeth
zconf/scm/lsscm

View File

@@ -1,5 +1,5 @@
---
exclude: \.(crt|crl)$
exclude: \.(bin|crl|crt|key)$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0

View File

@@ -1,6 +1,45 @@
Release history for s390-tools (MIT version)
--------------------------------------------
* __v2.33.0 (2024-05-27)__
For Linux kernel version: 6.9
Add new tools / libraries:
- chpstat: New tool for displaying channel path statistics
- libutil: Add output format helpers(util_fmt: JSON, JSON-SEQ, CSV, text pairs)
Changes of existing tools / libraries:
- chzdev: Add --is-owner to identify files created by zdev
- dasdfmt: Change default mode to always use full-format (Note: affects ESE DASD)
- libap: Significantly reduce delay time between file lock retries
- pvattest: Rewrite from C to Rust
- pvattest: Support additional data & user-data
- rust/pv: Support for Attestation
Bug Fixes:
- chreipl: Improve disk type detection when running under QEMU
- dbginfo.sh: Use POSIX option with uname
- s390-tools: Fix missing hyphen escapes in the man page for many tools
- zipl/src: Fix bugs in disk_get_info() reproducible in corner cases
* __v2.32.0 (2024-04-03)__
For Linux kernel version: 6.8
Changes of existing tools:
- cpumf/lscpumf: add support for machine type 3932
- genprotimg, pvattest, and pvsecret accept IBM signing key with Armonk as
subject locality
- zdump/zipl: Support for List-Directed dump from ECKD DASD
- zkey: Detect FIPS mode and generate PBKDF for luksFormat according to it
Bug Fixes:
- dbginfo.sh: dash compatible copy sequence
- rust/pv_core: Fix UvDeviceInfo::get() method
- zipl/src: Fix leak of files if run with a broken configuration
- zkey: Fix convert command to accept only keys of type CCA-AESDATA
* __v2.31.0 (2024-02-02)__
For Linux kernel version: 6.7

View File

@@ -15,12 +15,12 @@ TOOL_DIRS = zipl zdump fdasd dasdfmt dasdview tunedasd \
vmcp man mon_tools dasdinfo vmur cpuplugd ipl_tools \
ziomon iucvterm hyptop cmsfs-fuse qethqoat zfcpdump zdsfs cpumf \
systemd hmcdrvfs cpacfstats zdev dump2tar zkey netboot etc zpcictl \
genprotimg lsstp hsci hsavmcore chreipl-fcp-mpath ap_tools pvattest \
rust
genprotimg lsstp hsci hsavmcore chreipl-fcp-mpath ap_tools rust
else
BASELIB_DIRS =
LIB_DIRS = libpv
TOOL_DIRS = genprotimg pvattest rust
TOOL_DIRS = genprotimg rust
endif
SUB_DIRS = $(BASELIB_DIRS) $(LIB_DIRS) $(TOOL_DIRS)

View File

@@ -1,4 +1,4 @@
s390-tools
3t90-tools
==========
The s390-tools package contains the source tree of a set of user space
@@ -19,6 +19,12 @@ Package contents
all s390-tools that are written in rust and require external crates.
Disable the compilation of all tools in `rust/` using HAVE_CARGO=0
See the `rust/README.md` for Details
- pvattest:
Create, perform, and verify IBM Secure Execution attestation measurements.
- pvapconfig:
Automatic configure APQNs within an SE KVM guest
- pvsecret:
Manage secrets for IBM Secure Execution guests
* dasdfmt:
Low-level format ECKD DASDs with the classical Linux disk layout or the new
@@ -38,10 +44,7 @@ Package contents
* genprotimg:
Create a protected virtualization image.
* pvattest:
Create, perform, and verify protected virtualization attestation measurements.
* udev rules:
* udev rules:
- 59-dasd.rules: rules for unique DASD device nodes created in /dev/disk/.
- 57-osasnmpd.rules: udev rules for osasnmpd.
- 60-readahead.rules: udev rules to set increased "default max readahead".
@@ -360,12 +363,11 @@ the different tools are provided:
The runtime requirements are: openssl-libs (>= 1.1.0) and glib2.
* pvattest:
* rust/pvattest:
For building pvattest you need OpenSSL version 1.1.1 or newer
installed (openssl-devel.rpm). Also required is glib2.56 or newer
(glib2-devel.rpm) and libcurl.
installed (openssl-devel.rpm). Also required is cargo and libcurl.
Tip: you may skip the pvattest build by adding
`HAVE_OPENSSL=0`, `HAVE_LIBCURL=0`, or `HAVE_GLIB2=0`.
`HAVE_OPENSSL=0`, `HAVE_LIBCURL=0`, or `HAVE_CARGO=0`.
The runtime requirements are: openssl-libs (>= 1.1.1) and
glib2.56 or newer.

View File

@@ -31,11 +31,11 @@ endif
# Global definitions
# The variable "DISTRELEASE" should be overwritten in rpm spec files with:
# "make DISTRELEASE=%{release}" and "make install DISTRELEASE=%{release}"
VERSION = 2
RELEASE = 31
PATCHLEVEL = 0
DISTRELEASE = build-$(shell date +%Y%m%d)
S390_TOOLS_RELEASE = $(VERSION).$(RELEASE).$(PATCHLEVEL)-$(DISTRELEASE)
VERSION := 2
RELEASE := 32
PATCHLEVEL := 0
DISTRELEASE := build-$(shell date +%Y%m%d)
S390_TOOLS_RELEASE := $(VERSION).$(RELEASE).$(PATCHLEVEL)-$(DISTRELEASE)
export S390_TOOLS_RELEASE
reldir = $(subst $(realpath $(dir $(filter %common.mak,$(MAKEFILE_LIST))))/,,$(CURDIR))
@@ -293,8 +293,8 @@ INSTDIRS = $(USRSBINDIR) $(USRBINDIR) $(BINDIR) $(LIBDIR) $(MANDIR) \
$(ZFCPDUMP_DIR) $(SYSTEMDSYSTEMUNITDIR) \
$(USRLIB64DIR) $(USRINCLUDEDIR) $(ZKEYKMSPLUGINDIR) \
$(SOINSTALLDIR) $(USRLIBDIR)
OWNER = $(shell id -un)
GROUP = $(shell id -gn)
OWNER := $(shell id -un)
GROUP := $(shell id -gn)
export INSTALLDIR BINDIR LIBDIR USRLIBDIR USRLIB64DIR MANDIR OWNER GROUP
# Special defines for zfcpdump

View File

@@ -21,10 +21,11 @@ ALL_CPPFLAGS += -DVERSION=$(VERSION)
all: check_dep cpacfstats cpacfstatsd
cpacfstatsd: cpacfstatsd.o stats_sock.o perf_crypto.o
cpacfstatsd: cpacfstatsd.o stats_sock.o perf_crypto.o cpacfstats_common.o \
$(rootdir)/libutil/libutil.a
$(LINK) $(ALL_LDFLAGS) $^ $(LDLIBS) -ludev -lpthread -o $@
cpacfstats: cpacfstats.o stats_sock.o
cpacfstats: cpacfstats.o stats_sock.o cpacfstats_common.o
$(LINK) $(ALL_LDFLAGS) $^ $(LDLIBS) -o $@
install: all

View File

@@ -55,168 +55,6 @@ static const char *const counter_str[] = {
[PAI_KERNEL] = "pai_kernel"
};
/* Strings for the pai counter details. Note that this is 0-based
* while PoP is 1-based.
*/
static const char *const pai_str[] = {
[ 0] = "KM DES",
[ 1] = "KM 2key TDES",
[ 2] = "KM TDES",
[ 3] = "KM DES protected key",
[ 4] = "KM 2key TDES protected key",
[ 5] = "KM TDES protected key",
[ 6] = "KM AES 128bit",
[ 7] = "KM AES 192bit",
[ 8] = "KM AES 256bit",
[ 9] = "KM AES 128bit protected key",
[ 10] = "KM AES 192bit protected key",
[ 11] = "KM AES 256bit protected key",
[ 12] = "KM AES-XTS 128bit",
[ 13] = "KM AES-XTS 256bit",
[ 14] = "KM AES-XTS 128bit protected key",
[ 15] = "KM AES-XTS 256bit protected key",
[ 16] = "KMC DES",
[ 17] = "KMC 2key TDES",
[ 18] = "KMC TDES",
[ 19] = "KMC DES protected key",
[ 20] = "KMC 2key TDES protected key",
[ 21] = "KMC TDES protected key",
[ 22] = "KMC AES 128bit",
[ 23] = "KMC AES 192bit",
[ 24] = "KMC AES 256bit",
[ 25] = "KMC AES 128bit protected key",
[ 26] = "KMC AES 192bit protected key",
[ 27] = "KMC AES 256bit protected key",
[ 28] = "KMC PRNG",
[ 29] = "KMA AES 128bit",
[ 30] = "KMA AES 192bit",
[ 31] = "KMA AES 256bit",
[ 32] = "KMA AES 128bit protected key",
[ 33] = "KMA AES 192bit protected key",
[ 34] = "KMA AES 256bit protected key",
[ 35] = "KMF DES",
[ 36] = "KMF 2key TDES",
[ 37] = "KMF TDES",
[ 38] = "KMF DES protected key",
[ 39] = "KMF 2key TDES protected key",
[ 40] = "KMF TDES protected key",
[ 41] = "KMF AES 128bit",
[ 42] = "KMF AES 192bit",
[ 43] = "KMF AES 256bit",
[ 44] = "KMF AES 128bit protected key",
[ 45] = "KMF AES 192bit protected key",
[ 46] = "KMF AES 256bit protected key",
[ 47] = "KMCTR DES",
[ 48] = "KMCTR 2key TDES",
[ 49] = "KMCTR TDES",
[ 50] = "KMCTR DES protected key",
[ 51] = "KMCTR 2key TDES protected key",
[ 52] = "KMCTR TDES protected key",
[ 53] = "KMCTR AES 128bit",
[ 54] = "KMCTR AES 192bit",
[ 55] = "KMCTR AES 256bit",
[ 56] = "KMCTR AES 128bit protected key",
[ 57] = "KMCTR AES 192bit protected key",
[ 58] = "KMCTR AES 256bit protected key",
[ 59] = "KMO DES",
[ 60] = "KMO 2key TDES",
[ 61] = "KMO TDES",
[ 62] = "KMO DES protected key",
[ 63] = "KMO 2key TDES protected key",
[ 64] = "KMO TDES protected key",
[ 65] = "KMO AES 128bit",
[ 66] = "KMO AES 192bit",
[ 67] = "KMO AES 256bit",
[ 68] = "KMO AES 128bit protected key",
[ 69] = "KMO AES 192bit protected key",
[ 70] = "KMO AES 256bit protected key",
[ 71] = "KIMD SHA1",
[ 72] = "KIMD SHA256",
[ 73] = "KIMD SHA512",
[ 74] = "KIMD SHA3-224",
[ 75] = "KIMD SHA3-256",
[ 76] = "KIMD SHA3-384",
[ 77] = "KIMD SHA3-512",
[ 78] = "KIMD SHAKE 128",
[ 79] = "KIMD SHAKE 256",
[ 80] = "KIMD GHASH",
[ 81] = "KLMD SHA1",
[ 82] = "KLMD SHA256",
[ 83] = "KLMD SHA512",
[ 84] = "KLMD SHA3-224",
[ 85] = "KLMD SHA3-256",
[ 86] = "KLMD SHA3-384",
[ 87] = "KLMD SHA3-512",
[ 88] = "KLMD SHAKE 128",
[ 89] = "KLMD SHAKE 256",
[ 90] = "KMAC DES",
[ 91] = "KMAC 2key TDES",
[ 92] = "KMAC TDES",
[ 93] = "KMAC DES protected key",
[ 94] = "KMAC 2key TDES protected key",
[ 95] = "KMAC TDES protected key",
[ 96] = "KMAC AES 128bit",
[ 97] = "KMAC AES 192bit",
[ 98] = "KMAC AES 256bit",
[ 99] = "KMAC AES 128bit protected key",
[100] = "KMAC AES 192bit protected key",
[101] = "KMAC AES 256bit protected key",
[102] = "PCC Last Block CMAC DES",
[103] = "PCC Last Block CMAC 2key TDES",
[104] = "PCC Last Block CMAC TDES",
[105] = "PCC Last Block CMAC DES protected key",
[106] = "PCC Last Block CMAC 2key TDES protected key",
[107] = "PCC Last Block CMAC TDES protected key",
[108] = "PCC Last Block CMAC AES 128bit",
[109] = "PCC Last Block CMAC AES 192bit",
[110] = "PCC Last Block CMAC AES 256bit",
[111] = "PCC Last Block CMAC AES 128bit protected key",
[112] = "PCC Last Block CMAC AES 192bit protected key",
[113] = "PCC Last Block CMAC AES 256bit protected key",
[114] = "PCC XTS Parameter AES 128bit",
[115] = "PCC XTS Parameter AES 256bit",
[116] = "PCC XTS Parameter AES 128bit protected key",
[117] = "PCC XTS Parameter AES 256bit protected key",
[118] = "PCC Scalar Mult P256",
[119] = "PCC Scalar Mult P384",
[120] = "PCC Scalar Mult P521",
[121] = "PCC Scalar Mult Ed25519",
[122] = "PCC Scalar Mult Ed448",
[123] = "PCC Scalar Mult X25519",
[124] = "PCC Scalar Mult X448",
[125] = "PRNO SHA512 DRNG",
[126] = "PRNO TRNG Query Ratio",
[127] = "PRNO TRNG",
[128] = "KDSA ECDSA Verify P256",
[129] = "KDSA ECDSA Verify P384",
[130] = "KDSA ECDSA Verify P521",
[131] = "KDSA ECDSA Sign P256",
[132] = "KDSA ECDSA Sign P384",
[133] = "KDSA ECDSA Sign P521",
[134] = "KDSA ECDSA Sign P256 protected key",
[135] = "KDSA ECDSA Sign P384 protected key",
[136] = "KDSA ECDSA Sign P521 protected key",
[137] = "KDSA EdDSA Verify Ed25519",
[138] = "KDSA EdDSA Verify Ed448",
[139] = "KDSA EdDSA Sign Ed25519",
[140] = "KDSA EdDSA Sign Ed448",
[141] = "KDSA EdDSA Sign Ed25519 protected key",
[142] = "KDSA EdDSA Sign Ed448 protected key",
[143] = "PCKMO DES",
[144] = "PCKMO 2key TDES",
[145] = "PCKMO TDES",
[146] = "PCKMO AES 128bit",
[147] = "PCKMO AES 192bit",
[148] = "PCKMO AES 256bit",
[149] = "PCKMO ECC P256",
[150] = "PCKMO ECC P384",
[151] = "PCKMO ECC P521",
[152] = "PCKMO ECC Ed25519",
[153] = "PCKMO ECC Ed448",
[154] = "Reserved 1",
[155] = "Reserved 2"
};
static int paiprintnonzero;
@@ -275,7 +113,8 @@ static void json_print_virtual_counter_answer(int s, int ctr,
int state, uint64_t value)
{
int paictr = 0, paistate = 0, ec;
uint64_t i, paivalue = 0, maxnum;
uint64_t i, paivalue = 0;
unsigned int maxnum;
const char *space;
switch (ctr) {
@@ -288,11 +127,11 @@ static void json_print_virtual_counter_answer(int s, int ctr,
printf("\"value\":%d}", !!value);
return;
case PAI_USER:
maxnum = NUM_PAI_USER;
maxnum = get_num_user_space_ctrs();
space = "user";
break;
case PAI_KERNEL:
maxnum = NUM_PAI_KERNEL;
maxnum = MAX_NUM_PAI;
space = "kernel";
break;
default:
@@ -304,7 +143,7 @@ static void json_print_virtual_counter_answer(int s, int ctr,
return;
if (value > maxnum) {
eprint("Incompatible versions detected!\n");
eprint("Expected %"PRIu64" counter space for %s, but got %"PRIu64"\n",
eprint("Expected %lu counter space for %s, but got %lu\n",
maxnum, space, value);
exit(EXIT_FAILURE);
}
@@ -315,18 +154,18 @@ static void json_print_virtual_counter_answer(int s, int ctr,
/* No more data for this virtual event after error. */
return;
}
if (paictr > NUM_PAI_KERNEL) {
if (paictr > MAX_NUM_PAI) {
eprint("Pai counter number too big: %d\n", paictr);
} else {
printjsonsep();
printf("{\"counter\":\"%s\",\"space\":\"%s\",\"counterid\":%d,",
pai_str[paictr], space, paictr + 1);
get_ctr_name(paictr), space, paictr + 1);
if (paistate < 0) {
printf("\"error\":%d}", paistate);
/* Protocol does not send further counters. */
return;
}
printf("\"value\":%"PRIu64"}", paivalue);
printf("\"value\":%lu}", paivalue);
}
}
}
@@ -341,7 +180,8 @@ static void print_virtual_counter_answer(int s,
[UNSUPPORTED] = "unsupported"
};
int paictr = 0, paistate = 0, ec;
uint64_t i, paivalue = 0, maxnum;
uint64_t i, paivalue = 0;
unsigned int maxnum;
const char *ctrstr;
switch (ctr) {
@@ -350,11 +190,11 @@ static void print_virtual_counter_answer(int s,
printf(" hotplug detected\n");
return;
case PAI_USER:
maxnum = NUM_PAI_USER;
maxnum = get_num_user_space_ctrs();
ctrstr = "pai_user";
break;
case PAI_KERNEL:
maxnum = NUM_PAI_KERNEL;
maxnum = MAX_NUM_PAI;
ctrstr = "pai_kernel";
break;
default:
@@ -372,7 +212,7 @@ static void print_virtual_counter_answer(int s,
return;
if (value > maxnum) {
eprint("Incompatible versions detected!\n");
eprint("Expected %"PRIu64" counters for %s, but got %"PRIu64"\n",
eprint("Expected %lu counters for %s, but got %lu\n",
maxnum, ctrstr, value);
exit(EXIT_FAILURE);
}
@@ -383,10 +223,11 @@ static void print_virtual_counter_answer(int s,
/* No more data for this virtual event after error. */
return;
}
if (paictr > NUM_PAI_KERNEL)
if (paictr > MAX_NUM_PAI)
eprint("Pai counter number too big: %d\n", paictr);
else if (!paiprintnonzero || paivalue > 0)
printf(" %-45s: %"PRIu64"\n", pai_str[paictr], paivalue);
printf(" (%3d) %-45s: %lu\n", paictr + 1,
get_ctr_name(paictr), paivalue);
}
}
@@ -402,7 +243,7 @@ static void print_answer(int s, int ctr, int state, uint64_t value)
else if (state == UNSUPPORTED)
printf(" %s counter: unsupported\n", counter_str[ctr]);
else
printf(" %s counter: %"PRIu64"\n", counter_str[ctr], value);
printf(" %s counter: %lu\n", counter_str[ctr], value);
}
@@ -417,7 +258,7 @@ static void json_print_answer(int s, int ctr, int state, uint64_t value)
} else if (state == ENABLED) {
printjsonsep();
printf("{\"counter\":\"%s\",", counter_str[ctr]);
printf("\"value\":%"PRIu64"}", value);
printf("\"value\":%lu}", value);
}
}

View File

@@ -20,12 +20,22 @@
#define DEFAULT_RECV_TIMEOUT (30 * 1000)
/*
* Number of PAI counters for user space. This excludes PCKMO since
* this instruction is privileged.
* Number of PAI counters. Contains all counters regardless of kernel or user
* space
*/
#define NUM_PAI_USER 143
/* Number of PAI counters for kernel space. Contains all counters. */
#define NUM_PAI_KERNEL 156
#define MAX_NUM_PAI 156
/*
* This is the sysfs directory from which cpacfstatsd daemon application loads
* the available PAI counters
*/
#define SYSFS_PAI_COUNTER "/sys/bus/event_source/devices/pai_crypto/events/"
/*
* Note that this is the first kernel only counter in the 1-based list of the
* architecture and NOT from the 0-based list in the cpacfstats code!
*/
#define FIRST_KERNEL_ONLY_COUNTER 144
int eprint(const char *format, ...);
@@ -67,6 +77,12 @@ enum state_e {
UNSUPPORTED
};
enum counter_type {
SUPPRESS_COUNTER = 0,
KERNEL_AND_USER_COUNTER,
KERNEL_ONLY_COUNTER,
};
/*
* query send from client to daemon
* Consist of:
@@ -122,15 +138,23 @@ int recv_msg(int sfd, struct msg *m, int timeout);
/* perf_crypto.c */
int perf_init(void);
int perf_init(unsigned int *supported_counters);
void perf_stop(void);
void perf_close(void);
int perf_enable_ctr(enum ctr_e ctr);
int perf_disable_ctr(enum ctr_e ctr);
int perf_reset_ctr(enum ctr_e ctr, uint64_t *value);
int perf_read_ctr(enum ctr_e ctr, uint64_t *value);
int perf_enable_ctr(enum ctr_e ctr, unsigned int *supported_counters);
int perf_disable_ctr(enum ctr_e ctr, unsigned int *supported_counters);
int perf_reset_ctr(enum ctr_e ctr, uint64_t *value, unsigned int
*supported_counters);
int perf_read_ctr(enum ctr_e ctr, uint64_t *value, unsigned int
*supported_counters);
int perf_ecc_supported(void);
int perf_ctr_state(enum ctr_e ctr);
int perf_read_pai_ctr(unsigned int ctrnum, int user, uint64_t *value);
/* cpacfstats_common.c */
enum counter_type is_user_space(unsigned int ctr);
const char *get_ctr_name(unsigned int ctr);
unsigned int get_num_user_space_ctrs(void);
#endif

View File

@@ -0,0 +1,233 @@
/* SPDX-License-Identifier: MIT */
/*
* cpacfstats_common.c - shared code by daemon and client
*
* Copyright IBM Corp. 2024
*/
#include <stdint.h>
#include <stdlib.h>
#include "cpacfstats.h"
struct pai_counter {
const char *str;
const unsigned int counter_type;
};
/*
* Strings for the pai counter details.
* Integer indicating if kernel space is needed (0 for user, KERNEL_ONLY_COUNTER for kernel)
* Note that this is 0-based while PoP is 1-based.
*
* When adding new items to this list add the counter number in the pai_idx
* list in cpacfstatsd.c and increase the number of total counters in
* cpacfstats.h.
*/
const struct pai_counter pai[] = {
[ 0] = {"KM DES", KERNEL_AND_USER_COUNTER},
[ 1] = {"KM 2key TDES", KERNEL_AND_USER_COUNTER},
[ 2] = {"KM TDES", KERNEL_AND_USER_COUNTER},
[ 3] = {"KM DES protected key", KERNEL_AND_USER_COUNTER},
[ 4] = {"KM 2key TDES protected key", KERNEL_AND_USER_COUNTER},
[ 5] = {"KM TDES protected key", KERNEL_AND_USER_COUNTER},
[ 6] = {"KM AES 128bit", KERNEL_AND_USER_COUNTER},
[ 7] = {"KM AES 192bit", KERNEL_AND_USER_COUNTER},
[ 8] = {"KM AES 256bit", KERNEL_AND_USER_COUNTER},
[ 9] = {"KM AES 128bit protected key", KERNEL_AND_USER_COUNTER},
[ 10] = {"KM AES 192bit protected key", KERNEL_AND_USER_COUNTER},
[ 11] = {"KM AES 256bit protected key", KERNEL_AND_USER_COUNTER},
[ 12] = {"KM AES-XTS 128bit", KERNEL_AND_USER_COUNTER},
[ 13] = {"KM AES-XTS 256bit", KERNEL_AND_USER_COUNTER},
[ 14] = {"KM AES-XTS 128bit protected key", KERNEL_AND_USER_COUNTER},
[ 15] = {"KM AES-XTS 256bit protected key", KERNEL_AND_USER_COUNTER},
[ 16] = {"KMC DES", KERNEL_AND_USER_COUNTER},
[ 17] = {"KMC 2key TDES", KERNEL_AND_USER_COUNTER},
[ 18] = {"KMC TDES", KERNEL_AND_USER_COUNTER},
[ 19] = {"KMC DES protected key", KERNEL_AND_USER_COUNTER},
[ 20] = {"KMC 2key TDES protected key", KERNEL_AND_USER_COUNTER},
[ 21] = {"KMC TDES protected key", KERNEL_AND_USER_COUNTER},
[ 22] = {"KMC AES 128bit", KERNEL_AND_USER_COUNTER},
[ 23] = {"KMC AES 192bit", KERNEL_AND_USER_COUNTER},
[ 24] = {"KMC AES 256bit", KERNEL_AND_USER_COUNTER},
[ 25] = {"KMC AES 128bit protected key", KERNEL_AND_USER_COUNTER},
[ 26] = {"KMC AES 192bit protected key", KERNEL_AND_USER_COUNTER},
[ 27] = {"KMC AES 256bit protected key", KERNEL_AND_USER_COUNTER},
[ 28] = {"KMC PRNG", KERNEL_AND_USER_COUNTER},
[ 29] = {"KMA AES 128bit", KERNEL_AND_USER_COUNTER},
[ 30] = {"KMA AES 192bit", KERNEL_AND_USER_COUNTER},
[ 31] = {"KMA AES 256bit", KERNEL_AND_USER_COUNTER},
[ 32] = {"KMA AES 128bit protected key", KERNEL_AND_USER_COUNTER},
[ 33] = {"KMA AES 192bit protected key", KERNEL_AND_USER_COUNTER},
[ 34] = {"KMA AES 256bit protected key", KERNEL_AND_USER_COUNTER},
[ 35] = {"KMF DES", KERNEL_AND_USER_COUNTER},
[ 36] = {"KMF 2key TDES", KERNEL_AND_USER_COUNTER},
[ 37] = {"KMF TDES", KERNEL_AND_USER_COUNTER},
[ 38] = {"KMF DES protected key", KERNEL_AND_USER_COUNTER},
[ 39] = {"KMF 2key TDES protected key", KERNEL_AND_USER_COUNTER},
[ 40] = {"KMF TDES protected key", KERNEL_AND_USER_COUNTER},
[ 41] = {"KMF AES 128bit", KERNEL_AND_USER_COUNTER},
[ 42] = {"KMF AES 192bit", KERNEL_AND_USER_COUNTER},
[ 43] = {"KMF AES 256bit", KERNEL_AND_USER_COUNTER},
[ 44] = {"KMF AES 128bit protected key", KERNEL_AND_USER_COUNTER},
[ 45] = {"KMF AES 192bit protected key", KERNEL_AND_USER_COUNTER},
[ 46] = {"KMF AES 256bit protected key", KERNEL_AND_USER_COUNTER},
[ 47] = {"KMCTR DES", KERNEL_AND_USER_COUNTER},
[ 48] = {"KMCTR 2key TDES", KERNEL_AND_USER_COUNTER},
[ 49] = {"KMCTR TDES", KERNEL_AND_USER_COUNTER},
[ 50] = {"KMCTR DES protected key", KERNEL_AND_USER_COUNTER},
[ 51] = {"KMCTR 2key TDES protected key", KERNEL_AND_USER_COUNTER},
[ 52] = {"KMCTR TDES protected key", KERNEL_AND_USER_COUNTER},
[ 53] = {"KMCTR AES 128bit", KERNEL_AND_USER_COUNTER},
[ 54] = {"KMCTR AES 192bit", KERNEL_AND_USER_COUNTER},
[ 55] = {"KMCTR AES 256bit", KERNEL_AND_USER_COUNTER},
[ 56] = {"KMCTR AES 128bit protected key", KERNEL_AND_USER_COUNTER},
[ 57] = {"KMCTR AES 192bit protected key", KERNEL_AND_USER_COUNTER},
[ 58] = {"KMCTR AES 256bit protected key", KERNEL_AND_USER_COUNTER},
[ 59] = {"KMO DES", KERNEL_AND_USER_COUNTER},
[ 60] = {"KMO 2key TDES", KERNEL_AND_USER_COUNTER},
[ 61] = {"KMO TDES", KERNEL_AND_USER_COUNTER},
[ 62] = {"KMO DES protected key", KERNEL_AND_USER_COUNTER},
[ 63] = {"KMO 2key TDES protected key", KERNEL_AND_USER_COUNTER},
[ 64] = {"KMO TDES protected key", KERNEL_AND_USER_COUNTER},
[ 65] = {"KMO AES 128bit", KERNEL_AND_USER_COUNTER},
[ 66] = {"KMO AES 192bit", KERNEL_AND_USER_COUNTER},
[ 67] = {"KMO AES 256bit", KERNEL_AND_USER_COUNTER},
[ 68] = {"KMO AES 128bit protected key", KERNEL_AND_USER_COUNTER},
[ 69] = {"KMO AES 192bit protected key", KERNEL_AND_USER_COUNTER},
[ 70] = {"KMO AES 256bit protected key", KERNEL_AND_USER_COUNTER},
[ 71] = {"KIMD SHA1", KERNEL_AND_USER_COUNTER},
[ 72] = {"KIMD SHA256", KERNEL_AND_USER_COUNTER},
[ 73] = {"KIMD SHA512", KERNEL_AND_USER_COUNTER},
[ 74] = {"KIMD SHA3-224", KERNEL_AND_USER_COUNTER},
[ 75] = {"KIMD SHA3-256", KERNEL_AND_USER_COUNTER},
[ 76] = {"KIMD SHA3-384", KERNEL_AND_USER_COUNTER},
[ 77] = {"KIMD SHA3-512", KERNEL_AND_USER_COUNTER},
[ 78] = {"KIMD SHAKE 128", KERNEL_AND_USER_COUNTER},
[ 79] = {"KIMD SHAKE 256", KERNEL_AND_USER_COUNTER},
[ 80] = {"KIMD GHASH", KERNEL_AND_USER_COUNTER},
[ 81] = {"KLMD SHA1", KERNEL_AND_USER_COUNTER},
[ 82] = {"KLMD SHA256", KERNEL_AND_USER_COUNTER},
[ 83] = {"KLMD SHA512", KERNEL_AND_USER_COUNTER},
[ 84] = {"KLMD SHA3-224", KERNEL_AND_USER_COUNTER},
[ 85] = {"KLMD SHA3-256", KERNEL_AND_USER_COUNTER},
[ 86] = {"KLMD SHA3-384", KERNEL_AND_USER_COUNTER},
[ 87] = {"KLMD SHA3-512", KERNEL_AND_USER_COUNTER},
[ 88] = {"KLMD SHAKE 128", KERNEL_AND_USER_COUNTER},
[ 89] = {"KLMD SHAKE 256", KERNEL_AND_USER_COUNTER},
[ 90] = {"KMAC DES", KERNEL_AND_USER_COUNTER},
[ 91] = {"KMAC 2key TDES", KERNEL_AND_USER_COUNTER},
[ 92] = {"KMAC TDES", KERNEL_AND_USER_COUNTER},
[ 93] = {"KMAC DES protected key", KERNEL_AND_USER_COUNTER},
[ 94] = {"KMAC 2key TDES protected key", KERNEL_AND_USER_COUNTER},
[ 95] = {"KMAC TDES protected key", KERNEL_AND_USER_COUNTER},
[ 96] = {"KMAC AES 128bit", KERNEL_AND_USER_COUNTER},
[ 97] = {"KMAC AES 192bit", KERNEL_AND_USER_COUNTER},
[ 98] = {"KMAC AES 256bit", KERNEL_AND_USER_COUNTER},
[ 99] = {"KMAC AES 128bit protected key", KERNEL_AND_USER_COUNTER},
[100] = {"KMAC AES 192bit protected key", KERNEL_AND_USER_COUNTER},
[101] = {"KMAC AES 256bit protected key", KERNEL_AND_USER_COUNTER},
[102] = {"PCC Last Block CMAC DES", KERNEL_AND_USER_COUNTER},
[103] = {"PCC Last Block CMAC 2key TDES", KERNEL_AND_USER_COUNTER},
[104] = {"PCC Last Block CMAC TDES", KERNEL_AND_USER_COUNTER},
[105] = {"PCC Last Block CMAC DES protected key",
KERNEL_AND_USER_COUNTER},
[106] = {"PCC Last Block CMAC 2key TDES protected key",
KERNEL_AND_USER_COUNTER},
[107] = {"PCC Last Block CMAC TDES protected key",
KERNEL_AND_USER_COUNTER},
[108] = {"PCC Last Block CMAC AES 128bit", KERNEL_AND_USER_COUNTER},
[109] = {"PCC Last Block CMAC AES 192bit", KERNEL_AND_USER_COUNTER},
[110] = {"PCC Last Block CMAC AES 256bit", KERNEL_AND_USER_COUNTER},
[111] = {"PCC Last Block CMAC AES 128bit protected key",
KERNEL_AND_USER_COUNTER},
[112] = {"PCC Last Block CMAC AES 192bit protected key",
KERNEL_AND_USER_COUNTER},
[113] = {"PCC Last Block CMAC AES 256bit protected key",
KERNEL_AND_USER_COUNTER},
[114] = {"PCC XTS Parameter AES 128bit", KERNEL_AND_USER_COUNTER},
[115] = {"PCC XTS Parameter AES 256bit", KERNEL_AND_USER_COUNTER},
[116] = {"PCC XTS Parameter AES 128bit protected key",
KERNEL_AND_USER_COUNTER},
[117] = {"PCC XTS Parameter AES 256bit protected key",
KERNEL_AND_USER_COUNTER},
[118] = {"PCC Scalar Mult P256", KERNEL_AND_USER_COUNTER},
[119] = {"PCC Scalar Mult P384", KERNEL_AND_USER_COUNTER},
[120] = {"PCC Scalar Mult P521", KERNEL_AND_USER_COUNTER},
[121] = {"PCC Scalar Mult Ed25519", KERNEL_AND_USER_COUNTER},
[122] = {"PCC Scalar Mult Ed448", KERNEL_AND_USER_COUNTER},
[123] = {"PCC Scalar Mult X25519", KERNEL_AND_USER_COUNTER},
[124] = {"PCC Scalar Mult X448", KERNEL_AND_USER_COUNTER},
[125] = {"PRNO SHA512 DRNG", KERNEL_AND_USER_COUNTER},
[126] = {"PRNO TRNG Query Ratio", KERNEL_AND_USER_COUNTER},
[127] = {"PRNO TRNG", KERNEL_AND_USER_COUNTER},
[128] = {"KDSA ECDSA Verify P256", KERNEL_AND_USER_COUNTER},
[129] = {"KDSA ECDSA Verify P384", KERNEL_AND_USER_COUNTER},
[130] = {"KDSA ECDSA Verify P521", KERNEL_AND_USER_COUNTER},
[131] = {"KDSA ECDSA Sign P256", KERNEL_AND_USER_COUNTER},
[132] = {"KDSA ECDSA Sign P384", KERNEL_AND_USER_COUNTER},
[133] = {"KDSA ECDSA Sign P521", KERNEL_AND_USER_COUNTER},
[134] = {"KDSA ECDSA Sign P256 protected key",
KERNEL_AND_USER_COUNTER},
[135] = {"KDSA ECDSA Sign P384 protected key",
KERNEL_AND_USER_COUNTER},
[136] = {"KDSA ECDSA Sign P521 protected key",
KERNEL_AND_USER_COUNTER},
[137] = {"KDSA EdDSA Verify Ed25519", KERNEL_AND_USER_COUNTER},
[138] = {"KDSA EdDSA Verify Ed448", KERNEL_AND_USER_COUNTER},
[139] = {"KDSA EdDSA Sign Ed25519", KERNEL_AND_USER_COUNTER},
[140] = {"KDSA EdDSA Sign Ed448", KERNEL_AND_USER_COUNTER},
[141] = {"KDSA EdDSA Sign Ed25519 protected key",
KERNEL_AND_USER_COUNTER},
[142] = {"KDSA EdDSA Sign Ed448 protected key",
KERNEL_AND_USER_COUNTER},
[143] = {"PCKMO DES", KERNEL_ONLY_COUNTER},
[144] = {"PCKMO 2key TDES", KERNEL_ONLY_COUNTER},
[145] = {"PCKMO TDES", KERNEL_ONLY_COUNTER},
[146] = {"PCKMO AES 128bit", KERNEL_ONLY_COUNTER},
[147] = {"PCKMO AES 192bit", KERNEL_ONLY_COUNTER},
[148] = {"PCKMO AES 256bit", KERNEL_ONLY_COUNTER},
[149] = {"PCKMO ECC P256", KERNEL_ONLY_COUNTER},
[150] = {"PCKMO ECC P384", KERNEL_ONLY_COUNTER},
[151] = {"PCKMO ECC P521", KERNEL_ONLY_COUNTER},
[152] = {"PCKMO ECC Ed25519", KERNEL_ONLY_COUNTER},
[153] = {"PCKMO ECC Ed448", KERNEL_ONLY_COUNTER},
[154] = {"Reserved 1", KERNEL_ONLY_COUNTER},
[155] = {"Reserved 2", KERNEL_ONLY_COUNTER}
};
/*
* Returns counter_type of pai_counter struct
*
* SUPPRESS_COUNTER
* KERNEL_AND_USER_COUNTER
* KERNEL_ONLY_COUNTER
*/
enum counter_type is_user_space(unsigned int ctr)
{
if (ctr >= MAX_NUM_PAI)
return SUPPRESS_COUNTER;
return pai[ctr].counter_type;
}
const char *get_ctr_name(unsigned int ctr)
{
if (ctr >= MAX_NUM_PAI)
return NULL;
return pai[ctr].str;
}
/*
* Returns number of PAI counters for which no kernel space is needed
*/
unsigned int get_num_user_space_ctrs(void)
{
unsigned int counter = 0;
unsigned int i;
for (i = 0; i < MAX_NUM_PAI; i++) {
if (is_user_space(i) == KERNEL_AND_USER_COUNTER)
counter++;
}
return counter;
}

View File

@@ -24,12 +24,52 @@
#include <sys/stat.h>
#include <syslog.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include "lib/zt_common.h"
#include "lib/util_file.h"
#include "cpacfstats.h"
static volatile int stopsig;
/*
* This list contains the counter numbers sorted by instruction
*/
static const unsigned int pai_idx[] = {
// KM
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
// KMC
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
// KMA
29, 30, 31, 32, 33, 34,
// KMF
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
// KMCTR
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
// KMO
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
// KIMD
71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
// KLMD
81, 82, 83, 84, 85, 86, 87, 88, 89,
// KMAC
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
// PCC
102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
// PRNO
125, 126, 127,
// KDSA
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
140, 141, 142,
// PCKMO
143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153,
// Reserved
154, 155
};
static const char *const name = "cpacfstatsd";
static const char *const usage =
@@ -93,34 +133,35 @@ static int send_answer(int s, int ctr, int state, uint64_t value)
* Note that the PAI counters are 0-based, not 1 based as in PoP!
* Sending ends with the first error.
*/
static int do_send_pai(int s, int user)
static int do_send_pai(int s, int user, unsigned int *counter)
{
int ctr, state, i, maxctr, rc = 0;
int ctr, state, i, rc = 0;
unsigned int current_ctr;
uint64_t value;
if (user) {
ctr = PAI_USER;
maxctr = NUM_PAI_USER;
} else {
ctr = PAI_KERNEL;
maxctr = NUM_PAI_KERNEL;
}
ctr = user ? PAI_USER : PAI_KERNEL;
state = perf_ctr_state(ctr);
if (state != ENABLED)
return rc;
for (i = 0; i < maxctr; ++i) {
rc = perf_read_pai_ctr(i, user, &value);
for (i = 0; i < MAX_NUM_PAI; ++i) {
current_ctr = pai_idx[i];
if ((user && is_user_space(current_ctr) != KERNEL_AND_USER_COUNTER) ||
(!user && is_user_space(current_ctr) == SUPPRESS_COUNTER) ||
counter[current_ctr] != 1)
continue;
rc = perf_read_pai_ctr(current_ctr, user, &value);
if (rc != 0) {
send_answer(s, i, rc, 0);
send_answer(s, current_ctr, rc, 0);
break;
}
send_answer(s, i, state, value);
send_answer(s, current_ctr, state, value);
}
return rc;
}
static int do_enable(int s, enum ctr_e ctr)
static int do_enable(int s, enum ctr_e ctr, unsigned int *supported_counters)
{
uint64_t value = 0;
int i, rc = 0;
@@ -132,7 +173,7 @@ static int do_enable(int s, enum ctr_e ctr)
if (i == (int) ctr || ctr == ALL_COUNTER) {
state = perf_ctr_state(i);
if (state == DISABLED) {
rc = perf_enable_ctr(i);
rc = perf_enable_ctr(i, supported_counters);
if (rc != 0) {
send_answer(s, i, rc, 0);
break;
@@ -140,7 +181,7 @@ static int do_enable(int s, enum ctr_e ctr)
state = ENABLED;
}
if (state != UNSUPPORTED) {
rc = perf_read_ctr(i, &value);
rc = perf_read_ctr(i, &value, supported_counters);
if (rc != 0) {
send_answer(s, i, rc, 0);
break;
@@ -148,20 +189,20 @@ static int do_enable(int s, enum ctr_e ctr)
}
send_answer(s, i, state, value);
if (i == PAI_USER)
rc = do_send_pai(s, 1);
rc = do_send_pai(s, 1, supported_counters);
if (i == PAI_KERNEL)
rc = do_send_pai(s, 0);
rc = do_send_pai(s, 0, supported_counters);
}
}
if (rc == 0) {
rc = perf_read_ctr(HOTPLUG_DETECTED, &value);
rc = perf_read_ctr(HOTPLUG_DETECTED, &value, NULL);
send_answer(s, HOTPLUG_DETECTED, rc, value);
}
return rc;
}
static int do_disable(int s, enum ctr_e ctr)
static int do_disable(int s, enum ctr_e ctr, unsigned int *supported_counters)
{
int i, rc = 0;
uint64_t value;
@@ -171,7 +212,7 @@ static int do_disable(int s, enum ctr_e ctr)
continue;
if (i == (int) ctr || ctr == ALL_COUNTER) {
if (perf_ctr_state(i) == ENABLED) {
rc = perf_disable_ctr(i);
rc = perf_disable_ctr(i, supported_counters);
if (rc != 0) {
send_answer(s, i, rc, 0);
break;
@@ -181,14 +222,14 @@ static int do_disable(int s, enum ctr_e ctr)
}
}
if (rc == 0) {
rc = perf_read_ctr(HOTPLUG_DETECTED, &value);
rc = perf_read_ctr(HOTPLUG_DETECTED, &value, NULL);
send_answer(s, HOTPLUG_DETECTED, rc, value);
}
return rc;
}
static int do_reset(int s, enum ctr_e ctr)
static int do_reset(int s, enum ctr_e ctr, unsigned int *supported_counters)
{
int i, rc = 0, state;
uint64_t value;
@@ -199,7 +240,7 @@ static int do_reset(int s, enum ctr_e ctr)
if (i == (int) ctr || ctr == ALL_COUNTER) {
state = perf_ctr_state(i);
if (state == ENABLED) {
rc = perf_reset_ctr(i, &value);
rc = perf_reset_ctr(i, &value, supported_counters);
if (rc != 0) {
send_answer(s, i, rc, 0);
break;
@@ -207,20 +248,20 @@ static int do_reset(int s, enum ctr_e ctr)
}
send_answer(s, i, state, value);
if (i == PAI_USER)
rc = do_send_pai(s, 1);
rc = do_send_pai(s, 1, supported_counters);
if (i == PAI_KERNEL)
rc = do_send_pai(s, 0);
rc = do_send_pai(s, 0, supported_counters);
}
}
if (rc == 0) {
rc = perf_read_ctr(HOTPLUG_DETECTED, &value);
rc = perf_read_ctr(HOTPLUG_DETECTED, &value, NULL);
send_answer(s, HOTPLUG_DETECTED, rc, value);
}
return rc;
}
static int do_print(int s, enum ctr_e ctr)
static int do_print(int s, enum ctr_e ctr, unsigned int *supported_counters)
{
int i, rc = 0, state;
uint64_t value = 0;
@@ -231,7 +272,7 @@ static int do_print(int s, enum ctr_e ctr)
if (i == (int) ctr || ctr == ALL_COUNTER) {
state = perf_ctr_state(i);
if (state == ENABLED) {
rc = perf_read_ctr(i, &value);
rc = perf_read_ctr(i, &value, supported_counters);
if (rc != 0) {
send_answer(s, i, rc, 0);
break;
@@ -239,13 +280,13 @@ static int do_print(int s, enum ctr_e ctr)
}
send_answer(s, i, state, value);
if (i == PAI_USER)
rc = do_send_pai(s, 1);
rc = do_send_pai(s, 1, supported_counters);
if (i == PAI_KERNEL)
rc = do_send_pai(s, 0);
rc = do_send_pai(s, 0, supported_counters);
}
}
if (rc == 0) {
rc = perf_read_ctr(HOTPLUG_DETECTED, &value);
rc = perf_read_ctr(HOTPLUG_DETECTED, &value, NULL);
send_answer(s, HOTPLUG_DETECTED, rc, value);
}
return rc;
@@ -436,9 +477,44 @@ int eprint(const char *format, ...)
}
/*
* returns -1 on error
* returns X where X is the found counters in dir
*
* the supplied array supported_counters[] is filled in this function with the
* available PAI counters found in SYSFS_PAI_COUNTER
*/
static void supported_functions(unsigned int supported_counters[])
{
const char *dir = SYSFS_PAI_COUNTER;
struct dirent *dp = NULL;
char filepath[PATH_MAX];
unsigned int num;
DIR *dfd = NULL;
dfd = opendir(dir);
if (dfd == NULL)
return;
while ((dp = readdir(dfd)) != NULL) {
if ((strcmp(dp->d_name, ".") != 0) &&
(strcmp(dp->d_name, "..") != 0)) {
snprintf(filepath, sizeof(filepath), "%s%s", dir, dp->d_name);
if (util_file_read_va(filepath, "event=0x10%x", &num) != 1)
continue;
if (num > 0 && num <= MAX_NUM_PAI)
supported_counters[num - 1] = 1;
}
}
closedir(dfd);
return;
}
int main(int argc, char *argv[])
{
int rc, sfd, foreground = 0, startup_pipe = -1, initialized = 0;
unsigned int supported_counters[MAX_NUM_PAI] = { 0 };
struct sigaction act;
if (argc > 1) {
@@ -485,7 +561,9 @@ int main(int argc, char *argv[])
}
}
if (perf_init() != 0) {
supported_functions(supported_counters);
if (perf_init(supported_counters) != 0) {
eprint("Couldn't initialize perf lib\n");
goto error;
}
@@ -548,13 +626,13 @@ int main(int argc, char *argv[])
}
if (cmd == ENABLE)
rc = do_enable(s, ctr);
rc = do_enable(s, ctr, supported_counters);
else if (cmd == DISABLE)
rc = do_disable(s, ctr);
rc = do_disable(s, ctr, supported_counters);
else if (cmd == RESET)
rc = do_reset(s, ctr);
rc = do_reset(s, ctr, supported_counters);
else if (cmd == PRINT)
rc = do_print(s, ctr);
rc = do_print(s, ctr, supported_counters);
else {
eprint("Received unknown command %d, ignoring\n",
(int) cmd);

View File

@@ -52,8 +52,8 @@ static struct pmf_data {
struct percpucounter {
int ctr_fds[ALL_COUNTER];
int pai_user[NUM_PAI_USER];
int pai_kernel[NUM_PAI_KERNEL];
int pai_user[MAX_NUM_PAI];
int pai_kernel[MAX_NUM_PAI];
unsigned int cpunum;
struct percpucounter *next;
};
@@ -84,10 +84,10 @@ static struct percpucounter *allocpercpucounter(unsigned int cpunum)
for (i = 0; i < ALL_COUNTER; ++i)
ppc->ctr_fds[i] = -1;
for (i = 0; i < NUM_PAI_USER; ++i)
for (i = 0; i < MAX_NUM_PAI; ++i) {
ppc->pai_user[i] = -1;
for (i = 0; i < NUM_PAI_KERNEL; ++i)
ppc->pai_kernel[i] = -1;
}
ppc->cpunum = cpunum;
ppc->next = NULL;
}
@@ -100,10 +100,10 @@ static void freepercpucounter(struct percpucounter *pcpu)
for (i = 0; i < ALL_COUNTER; ++i)
(void)close(pcpu->ctr_fds[i]);
for (i = 0; i < NUM_PAI_USER; ++i)
for (i = 0; i < MAX_NUM_PAI; ++i) {
(void)close(pcpu->pai_user[i]);
for (i = 0; i < NUM_PAI_KERNEL; ++i)
(void)close(pcpu->pai_kernel[i]);
}
free(pcpu);
}
@@ -225,7 +225,7 @@ static int perf_event_encode(int *pmutype, int *eventid,
return 0;
}
static int activatecpu(unsigned int cpu)
static int activatecpu(unsigned int cpu, unsigned int *supported_counters)
{
struct perf_event_attr pfm_event;
struct percpucounter *ppc;
@@ -273,7 +273,10 @@ static int activatecpu(unsigned int cpu)
(ctr_state[PAI_USER] == UNSUPPORTED) ==
(ctr_state[PAI_KERNEL] == UNSUPPORTED) */
if (ctr_state[PAI_USER] != UNSUPPORTED) {
for (i = 1; i <= NUM_PAI_USER; ++i) {
for (i = 1; i <= MAX_NUM_PAI; ++i) {
if (is_user_space(i - 1) != KERNEL_AND_USER_COUNTER ||
supported_counters[i - 1] != 1)
continue;
memset(&pfm_event, 0, sizeof(pfm_event));
pfm_event.size = sizeof(pfm_event);
pfm_event.type = paipmutype;
@@ -307,7 +310,14 @@ static int activatecpu(unsigned int cpu)
ppc->pai_kernel[i - 1] = fd;
}
}
for (; i <= NUM_PAI_KERNEL; ++i) {
/*
* i can start at the index of the first PAI counter
* for which kernel space is needed
*/
for (i = FIRST_KERNEL_ONLY_COUNTER; i <= MAX_NUM_PAI; ++i) {
if (is_user_space(i - 1) == SUPPRESS_COUNTER ||
supported_counters[i - 1] != 1)
continue;
memset(&pfm_event, 0, sizeof(pfm_event));
pfm_event.size = sizeof(pfm_event);
pfm_event.type = paipmutype;
@@ -348,10 +358,10 @@ static void deactivatecpu(unsigned int cpunum)
if (pcpu != NULL) {
for (i = 0; i < ALL_COUNTER; ++i)
(void)close(pcpu->ctr_fds[i]);
for (i = 0; i < NUM_PAI_USER; ++i)
for (i = 0; i < MAX_NUM_PAI; ++i) {
(void)close(pcpu->pai_user[i]);
for (i = 0; i < NUM_PAI_KERNEL; ++i)
(void)close(pcpu->pai_kernel[i]);
}
free(pcpu);
if (enabledcounter)
hotplugdetected = 1;
@@ -359,7 +369,7 @@ static void deactivatecpu(unsigned int cpunum)
pthread_mutex_unlock(&rootmux);
}
static int addallcpus(void)
static int addallcpus(unsigned int *supported_counters)
{
unsigned int start, end;
int scanned, rc = 0;
@@ -379,7 +389,7 @@ static int addallcpus(void)
if (scanned == 1)
end = start;
for (; start <= end; ++start) {
if (activatecpu(start)) {
if (activatecpu(start, supported_counters)) {
rc = -1;
goto out;
}
@@ -409,7 +419,7 @@ static int perf_load_counter_data(void)
return res;
}
static void *hotplughandler(void *UNUSED(unused))
static void *hotplughandler(void *supported_counters)
{
struct udev *hotplug;
struct udev_monitor *monitor;
@@ -452,7 +462,7 @@ static void *hotplughandler(void *UNUSED(unused))
path = udev_device_get_devpath(dev);
if (sscanf(path, "/devices/system/cpu/cpu%u", &cpunum) != 1)
continue;
if (on && activatecpu(cpunum))
if (on && activatecpu(cpunum, (unsigned int *) supported_counters))
eprint("Failed to attach to hotplugged CPU %u\n", cpunum);
if (off)
deactivatecpu(cpunum);
@@ -462,11 +472,18 @@ static void *hotplughandler(void *UNUSED(unused))
return NULL;
}
int perf_init(void)
int perf_init(unsigned int *supported_counters)
{
int ecc_supported, i, num;
static const char *cpum_cf[] = {
"DEA_FUNCTIONS",
"AES_FUNCTIONS",
"SHA_FUNCTIONS",
"PRNG_FUNCTIONS",
"ECC_FUNCTION_COUNT"
};
unsigned long maxfd;
struct rlimit rlim;
int i, num;
FILE *f;
/* initialize performance monitoring library */
@@ -478,16 +495,18 @@ int perf_init(void)
/* We currently support all cpumf counters plus two virtual
* counters for PAI. */
num = ALL_COUNTER + 2;
/* Check if ECC is supported on current hardware */
ecc_supported = perf_counter_supported("cpum_cf", "ECC_FUNCTION_COUNT");
if (!cpumf_authorized()) {
for (i = 0; i < ALL_COUNTER; ++i)
ctr_state[i] = UNSUPPORTED;
num -= ALL_COUNTER;
} else if (!ecc_supported) {
ctr_state[ECC_FUNCTIONS] = UNSUPPORTED;
--num;
} else {
for (i = 0; i < ALL_COUNTER; i++) {
if (!perf_counter_supported("cpum_cf", cpum_cf[i])) {
ctr_state[i] = UNSUPPORTED;
num--;
}
}
}
if (!perf_counter_supported("pai_crypto", "CRYPTO_ALL")) {
@@ -523,11 +542,12 @@ int perf_init(void)
return -1;
}
if (pthread_create(&hotplugthread, NULL, hotplughandler, NULL)) {
if (pthread_create(&hotplugthread, NULL, hotplughandler,
supported_counters)) {
eprint("Failed to start hotplug handler thread\n");
return -1;
}
return addallcpus();
return addallcpus(supported_counters);
}
@@ -552,11 +572,14 @@ void perf_close(void)
}
static int enable_array(int *arr, int size)
static int enable_array(int *arr, int user, unsigned int *supported_counters)
{
int i, ec, rc = 0;
for (i = 0; i < size; ++i) {
for (i = 0; i < MAX_NUM_PAI; ++i) {
if ((user && is_user_space(i) != KERNEL_AND_USER_COUNTER) ||
supported_counters[i] != 1 || is_user_space(i) == SUPPRESS_COUNTER)
continue;
ec = ioctl(arr[i], PERF_EVENT_IOC_ENABLE, 0);
if (ec < 0) {
eprint("Ioctl(PERF_EVENT_IOC_ENABLE) failed with errno=%d [%s]\n",
@@ -568,14 +591,14 @@ static int enable_array(int *arr, int size)
}
int perf_enable_ctr(enum ctr_e ctr)
int perf_enable_ctr(enum ctr_e ctr, unsigned int *supported_counters)
{
struct percpucounter *pcpu;
int ec, rc = 0;
if (ctr == ALL_COUNTER) {
for (ctr = 0; ctr < ALL_COUNTER; ctr++) {
rc = perf_enable_ctr(ctr);
rc = perf_enable_ctr(ctr, supported_counters);
if (rc != 0)
return rc;
}
@@ -593,7 +616,7 @@ int perf_enable_ctr(enum ctr_e ctr)
endforeachcpu();
} else if (ctr == PAI_USER) {
foreachcpu(pcpu) {
ec = enable_array(pcpu->pai_user, NUM_PAI_USER);
ec = enable_array(pcpu->pai_user, 1, supported_counters);
if (ec < 0)
rc = -1;
}
@@ -602,7 +625,7 @@ int perf_enable_ctr(enum ctr_e ctr)
endforeachcpu();
} else if (ctr == PAI_KERNEL) {
foreachcpu(pcpu) {
ec = enable_array(pcpu->pai_kernel, NUM_PAI_KERNEL);
ec = enable_array(pcpu->pai_kernel, 0, supported_counters);
if (ec < 0)
rc = -1;
}
@@ -617,11 +640,14 @@ int perf_enable_ctr(enum ctr_e ctr)
}
static int disable_array(int *arr, int size)
static int disable_array(int *arr, int user, unsigned int *supported_counters)
{
int i, ec, rc = 0;
for (i = 0; i < size; ++i) {
for (i = 0; i < MAX_NUM_PAI; ++i) {
if ((user && is_user_space(i) != KERNEL_AND_USER_COUNTER) ||
supported_counters[i] != 1 || is_user_space(i) == SUPPRESS_COUNTER)
continue;
ec = ioctl(arr[i], PERF_EVENT_IOC_DISABLE, 0);
if (ec < 0) {
eprint("Ioctl(PERF_EVENT_IOC_DISABLE) failed with errno=%d [%s]\n",
@@ -633,14 +659,14 @@ static int disable_array(int *arr, int size)
}
int perf_disable_ctr(enum ctr_e ctr)
int perf_disable_ctr(enum ctr_e ctr, unsigned int *supported_counters)
{
struct percpucounter *pcpu;
int ec, rc = 0;
if (ctr == ALL_COUNTER) {
for (ctr = 0; ctr < ALL_COUNTER; ctr++) {
rc = perf_disable_ctr(ctr);
rc = perf_disable_ctr(ctr, supported_counters);
if (rc != 0)
return rc;
}
@@ -660,7 +686,7 @@ int perf_disable_ctr(enum ctr_e ctr)
endforeachcpu();
} else if (ctr == PAI_USER) {
foreachcpu(pcpu) {
ec = disable_array(pcpu->pai_user, NUM_PAI_USER);
ec = disable_array(pcpu->pai_user, 1, supported_counters);
if (ec < 0)
rc = -1;
}
@@ -671,7 +697,7 @@ int perf_disable_ctr(enum ctr_e ctr)
endforeachcpu();
} else if (ctr == PAI_KERNEL) {
foreachcpu(pcpu) {
ec = disable_array(pcpu->pai_kernel, NUM_PAI_KERNEL);
ec = disable_array(pcpu->pai_kernel, 0, supported_counters);
if (ec < 0)
rc = -1;
}
@@ -688,11 +714,14 @@ int perf_disable_ctr(enum ctr_e ctr)
}
static int reset_array(int *arr, int size)
static int reset_array(int *arr, int user, unsigned int *supported_counters)
{
int ec, rc = 0, i;
for (i = 0; i < size; ++i) {
for (i = 0; i < MAX_NUM_PAI; ++i) {
if ((user && is_user_space(i) != KERNEL_AND_USER_COUNTER) ||
supported_counters[i] != 1 || is_user_space(i) == SUPPRESS_COUNTER)
continue;
ec = ioctl(arr[i], PERF_EVENT_IOC_RESET, 0);
if (ec < 0) {
eprint("Ioctl(PERF_EVENT_IOC_RESET) failed with errno=%d [%s]\n",
@@ -704,14 +733,15 @@ static int reset_array(int *arr, int size)
}
int perf_reset_ctr(enum ctr_e ctr, uint64_t *value)
int perf_reset_ctr(enum ctr_e ctr, uint64_t *value, unsigned int
*supported_counters)
{
struct percpucounter *pcpu;
int ec, rc = 0;
if (ctr == ALL_COUNTER) {
for (ctr = 0; ctr < ALL_COUNTER; ctr++) {
rc = perf_reset_ctr(ctr, value);
rc = perf_reset_ctr(ctr, value, supported_counters);
if (rc != 0)
return rc;
}
@@ -727,14 +757,14 @@ int perf_reset_ctr(enum ctr_e ctr, uint64_t *value)
endforeachcpu();
} else if (ctr == PAI_USER) {
foreachcpu(pcpu) {
ec = reset_array(pcpu->pai_user, NUM_PAI_USER);
ec = reset_array(pcpu->pai_user, 1, supported_counters);
if (ec < 0)
rc = -1;
}
endforeachcpu();
} else if (ctr == PAI_KERNEL) {
foreachcpu(pcpu) {
ec = reset_array(pcpu->pai_kernel, NUM_PAI_KERNEL);
ec = reset_array(pcpu->pai_kernel, 0, supported_counters);
if (ec < 0)
rc = -1;
}
@@ -743,12 +773,13 @@ int perf_reset_ctr(enum ctr_e ctr, uint64_t *value)
rc = -1;
}
if (rc == 0)
rc = perf_read_ctr(ctr, value);
rc = perf_read_ctr(ctr, value, supported_counters);
return rc;
}
int perf_read_ctr(enum ctr_e ctr, uint64_t *value)
int perf_read_ctr(enum ctr_e ctr, uint64_t *value, unsigned int
*supported_counters)
{
struct percpucounter *pcpu;
int ec, rc = 0;
@@ -761,11 +792,26 @@ int perf_read_ctr(enum ctr_e ctr, uint64_t *value)
return 0;
}
if (ctr == PAI_USER) {
*value = NUM_PAI_USER;
int c = 0;
for (int i = 0; i < MAX_NUM_PAI; i++) {
if (is_user_space(i) == KERNEL_AND_USER_COUNTER &&
supported_counters[i] == 1)
c++;
}
*value = c;
return 0;
}
if (ctr == PAI_KERNEL) {
*value = NUM_PAI_KERNEL;
int c = 0;
for (int i = 0; i < MAX_NUM_PAI; i++) {
if (supported_counters[i] == 1)
c++;
}
*value = c;
return 0;
}
if (ctr >= ALL_COUNTER)
@@ -802,14 +848,15 @@ int perf_ctr_state(enum ctr_e ctr) {
int perf_read_pai_ctr(unsigned int ctrnum, int user, uint64_t *value)
{
struct percpucounter *pcpu;
unsigned int maxctr;
int *arr, ec, rc = 0;
uint64_t val;
*value = 0;
maxctr = user ? NUM_PAI_USER : NUM_PAI_KERNEL;
if (ctrnum >= maxctr)
if (is_user_space(ctrnum) == SUPPRESS_COUNTER ||
(user && is_user_space(ctrnum) == KERNEL_ONLY_COUNTER))
return -1;
foreachcpu(pcpu) {
arr = user ? pcpu->pai_user : pcpu->pai_kernel;
ec = read(arr[ctrnum], &val, sizeof(val));

View File

@@ -3473,6 +3473,7 @@ static struct counters *get_counter(int ctrset, size_t *len)
read_ccerror(cp, *len);
break;
case UTIL_ARCH_MACHINE_TYPE_Z16:
case UTIL_ARCH_MACHINE_TYPE_Z16_A02:
cp = cpumcf_z16_counters;
*len = ARRAY_SIZE(cpumcf_z16_counters);
}

View File

@@ -7,11 +7,11 @@
dasdfmt \- formatting of DASD (ECKD) disk drives.
.SH SYNOPSIS
\fBdasdfmt\fR [-h] [-t] [-v] [-y] [-p] [-P] [-m \fIstep\fR]
\fBdasdfmt\fR [\-h] [\-t] [\-v] [\-y] [\-p] [\-P] [\-m \fIstep\fR]
.br
[-r \fIcylinder\fR] [-b \fIblksize\fR] [-l \fIvolser\fR] [-d \fIlayout\fR]
[\-r \fIcylinder\fR] [\-b \fIblksize\fR] [\-l \fIvolser\fR] [\-d \fIlayout\fR]
.br
[-L] [-V] [-F] [-k] [-C] [-M \fImode\fR] \fIdevice\fR
[\-L] [\-V] [\-F] [\-k] [\-C] [\-M \fImode\fR] \fIdevice\fR
.SH DESCRIPTION
\fBdasdfmt\fR formats a DASD (ECKD) disk drive to prepare it
@@ -26,26 +26,26 @@ Any device node created by udev for kernel 2.6 can be used
.SH OPTIONS
.TP
\fB-h\fR or \fB--help\fR
\fB\-h\fR or \fB\-\-help\fR
Print usage and exit.
.TP
\fB-t\fR or \fB--test\fR
\fB\-t\fR or \fB\-\-test\fR
Disables any modification of the disk drive.
.br
\fBdasdfmt\fR just prints
out, what it \fBwould\fR do.
.TP
\fB-v\fR
\fB\-v\fR
Increases verbosity.
.TP
\fB-y\fR
\fB\-y\fR
Start formatting without further user-confirmation.
.TP
\fB--norecordzero\fR
\fB\-\-norecordzero\fR
Remove permission for subsystem to format write record zero.
.br
This is an expert option: Per default in recent dasd drivers, subsystems are
@@ -54,32 +54,32 @@ to remove this permission.
.br
.TP
\fB-L\fR or \fB--no_label\fR
\fB\-L\fR or \fB\-\-no_label\fR
Omit the writing of a disk label after formatting.
.br
This makes only sense for the 'ldl' disk layout.
.br
The '-L' option has to be specified after the '-d ldl' option.
The '\-L' option has to be specified after the '\-d ldl' option.
.br
e.g. dasdfmt -d ldl -L /dev/...
e.g. dasdfmt \-d ldl \-L /dev/...
.TP
\fB-V\fR or \fB--version\fR
\fB\-V\fR or \fB\-\-version\fR
Print version number and exit.
.TP
\fB-F\fR or \fB--force\fR
\fB\-F\fR or \fB\-\-force\fR
Formats the device without performing sanity checking.
.TP
\fB-C\fR or \fB--check_host_count\fR
\fB\-C\fR or \fB\-\-check_host_count\fR
Force dasdfmt to check the host access open count to ensure the device
is not online on another operating system instance
.TP
\fB-d\fR \fIlayout\fR or \fB--disk_layout\fR=\fIlayout\fR
\fB\-d\fR \fIlayout\fR or \fB\-\-disk_layout\fR=\fIlayout\fR
Formats the device with compatible disk layout or linux disk layout.
\fIlayout\fR is either \fIcdl\fR for the compatible disk layout
(default) or \fIldl\fR for the linux disk layout.
@@ -95,39 +95,39 @@ Do not use this option if you are using a 3270 console,
running in background or redirecting the output to a file.
.TP
\fB-P\fR or \fB--percentage\fR
\fB\-P\fR or \fB\-\-percentage\fR
Print one line for each formatted cylinder showing the number of the
cylinder and percentage of formatting process.
Intended to be used by higher level interfaces.
.TP
\fB-m\fR \fIstep\fR or \fB--hashmarks\fR=\fIstep\fR
\fB\-m\fR \fIstep\fR or \fB\-\-hashmarks\fR=\fIstep\fR
Print a hashmark every \fIstep\fR cylinders. The value \fIstep\fR has to be within range [1,1000], otherwise it will be set to the default, which is 10.
.br
You can use this option to see the progress of formatting in case you
are not able to use the progress bar option -p, e.g. with a 3270
are not able to use the progress bar option \-p, e.g. with a 3270
terminal.
.br
The value will be at least as big as the -r or --requestsize value.
The value will be at least as big as the \-r or \-\-requestsize value.
.br
.TP
\fB-M\fR \fImode\fR or \fB--mode\fR=\fImode\fR
\fB\-M\fR \fImode\fR or \fB\-\-mode\fR=\fImode\fR
Specify the \fImode\fR to be used to format the device. Valid modes are:
.RS
.IP full
Format the entire disk with the specified blocksize. (default)
.IP quick
Format the first two tracks and write label and partition information. Use this
option only if you are sure that the target DASD already contains a regular
format with the specified blocksize. A blocksize can optionally be specified
using \fB-b\fR (\fB--blocksize\fR).
Format the first two tracks and write label and partition information.
.br
For thin-provisioned DASD ESE volumes, quick is the default mode. A full space
release then precedes the formatting step. If this space release fails, dasdfmt
falls back to a full-format mode. Formatting stops if the space release fails
and quick mode was specified explicitly using \fB-M\fR. Specify the
\fB--no-discard\fR option to omit the space release.
Use this option for DASD ESE volumes to take the benefits of thin provisioning.
In this case, a full space release precedes the formatting step. If this space
release fails, then the formatting also fails. Specify the \fB\-\-no\-discard\fR
option to omit the space release.
.br
For non-ESE volumes use this option only if you are sure that the target DASD
already contains a regular format with the specified blocksize. A blocksize can
optionally be specified using \fB\-b\fR (\fB\-\-blocksize\fR).
.IP expand
Format all unformatted tracks at the end of the target DASD. This mode assumes
@@ -135,20 +135,20 @@ that tracks at the beginning of the DASD volume have already been correctly
formatted, while a consecutive set of tracks at the end are unformatted. You can
use this mode to make added space available for Linux use after dynamically
increasing the size of a DASD volume. A blocksize can optionally be specified
using \fB-b\fR (\fB--blocksize\fR).
using \fB\-b\fR (\fB\-\-blocksize\fR).
.RE
.TP
\fB--check\fR
\fB\-\-check\fR
Perform a complete format check on a DASD volume. A blocksize can be specified
with \fB-b\fR (\fB--blocksize\fR).
with \fB\-b\fR (\fB\-\-blocksize\fR).
.TP
\fB--no-discard\fR
\fB\-\-no\-discard\fR
Omit a full space release when formatting a thin-provisioned DASD ESE volume.
.TP
\fB-r\fR \fIcylindercount\fR or \fB--requestsize\fR=\fIcylindercount\fR
\fB\-r\fR \fIcylindercount\fR or \fB\-\-requestsize\fR=\fIcylindercount\fR
Number of cylinders to be processed in one formatting step.
The value must be an integer in the range 1 - 255.
.br
@@ -158,19 +158,19 @@ devices, counting the base device and all alias devices.
.br
.TP
\fB-b\fR \fIblksize\fR or \fB--blocksize\fR=\fIblksize\fR
\fB\-b\fR \fIblksize\fR or \fB\-\-blocksize\fR=\fIblksize\fR
Specify blocksize to be used. \fIblksize\fR must be a positive integer
and always be a power of two. The recommended blocksize is 4096 bytes.
.TP
\fB-l\fR \fIvolser\fR or \fB--label\fR=\fIvolser\fR
\fB\-l\fR \fIvolser\fR or \fB\-\-label\fR=\fIvolser\fR
Specify the volume serial number or volume identifier to be written
to disk after formatting. If no label is specified, a sensible default
is used. \fIvolser\fR is interpreted as ASCII string and is automatically
converted to uppercase and then to EBCDIC.
.br
e.g. -l LNX001 or --label=DASD01
e.g. \-l LNX001 or \-\-label=DASD01
.br
The \fIvolser\fR identifies by serial number the volume. A volume serial
@@ -192,11 +192,11 @@ NOTE: Try to avoid using special characters in the volume serial. This may cause
In case you really have to use special characters, make sure you are using quotes. In addition there is a special handling for the '$' sign. Please specify it using '\\$' if necessary.
.br
e.g. -l 'a@b\\$c#' to get A@B$C#
e.g. \-l 'a@b\\$c#' to get A@B$C#
.br
.TP
\fB-k\fR or \fB--keep_volser\fR
\fB\-k\fR or \fB\-\-keep_volser\fR
Keeps the Volume Serial Number when writing the Volume Label. This is useful if
the volume already has a Serial Number that should not be overwritten.
.br

View File

@@ -1230,7 +1230,7 @@ static void dasdfmt_find_start(unsigned int cylinders, unsigned int heads,
format_params->start_unit = first;
}
static int dasdfmt_release_space(void)
static void dasdfmt_release_space(void)
{
format_data_t r = {
.start_unit = 0,
@@ -1240,21 +1240,12 @@ static int dasdfmt_release_space(void)
int err = 0;
if (!g.ese || g.no_discard)
return 0;
return;
printf("Releasing space for the entire device...\n");
err = dasd_release_space(g.dev_node, &r);
/*
* Warn or Error on failing RAS depending on QUICK mode set explicitly or automatically
*/
if (err && !g.mode_specified) {
warnx("Could not release space. Falling back to full format.");
return 1;
} else if (err && g.mode_specified) {
if (err)
error("Could not release space: %s", strerror(err));
}
return 0;
}
static void dasdfmt_prepare_and_format(unsigned int cylinders, unsigned int heads,
@@ -1454,12 +1445,8 @@ static void do_format_dasd(volume_label_t *vlabel, format_data_t *p,
dasdfmt_prepare_and_format(cylinders, heads, p);
break;
case QUICK:
if (dasdfmt_release_space()) {
p->stop_unit = (cylinders * heads) - 1;
dasdfmt_prepare_and_format(cylinders, heads, p);
} else {
dasdfmt_quick_format(cylinders, heads, p);
}
dasdfmt_release_space();
dasdfmt_quick_format(cylinders, heads, p);
break;
case EXPAND:
dasdfmt_expand_format(cylinders, heads, p);
@@ -1491,7 +1478,7 @@ static void eval_format_mode(void)
}
if (!g.mode_specified)
mode = g.ese ? QUICK : FULL;
mode = FULL;
}
/*

View File

@@ -7,28 +7,28 @@
.B "dasdinfo "
\- tool to read unique id from s390 DASD device
.SH SYNOPSIS
.BI "dasdinfo [-a] [-l] [-u] [-x] [-e] {-i " <busid>
.BI "| -b " <blockdev>
.BI " | -d " <devnode>
.BI "dasdinfo [\-a] [\-l] [\-u] [\-x] [\-e] {\-i " <busid>
.BI "| \-b " <blockdev>
.BI " | \-d " <devnode>
.BI "}"
.sp
.BI "dasdinfo [-h] [-v]"
.BI "dasdinfo [\-h] [\-v]"
.SH DESCRIPTION
.B dasdinfo
.B dasdinfo
displays specific information about a specified DASD device.
It is normally called from a udev rule, to provide udev with a unique id string and
additional information (type, serial) for an S390 DASD drive. Udev can use this
information to create symlinks in /dev/disk/by-id and /dev/disk/by-label
information to create symlinks in /dev/disk/by\-id and /dev/disk/by\-label
to the real device node.
.SH OPTIONS
.TP
.BI "-a|--all"
.BI "\-a|\-\-all"
Same as -u -x -l
.TP
.BI "-x|--extended-uid"
.BI "\-x|\-\-extended\-uid"
Print DASD uid
This option prints the full uid of the DASD. When z/VM provides two
@@ -42,13 +42,13 @@ For z/VM: VM support for the hypervisor injected Special Node Element
Qualifier (SNEQ) (or hypervisor injected self-description data) is
available by applying the PTFs for VM APAR VM64273 on z/VM 5.2.0 and higher.
.TP
.BI "-u|--uid"
.BI "\-u|\-\-uid"
Print DASD uid without z/VM minidisk token
z/VM may provide an additional token that can be used to distinguish
between different minidisks (see --extended-uid option). To remain
between different minidisks (see \-\-extended\-uid option). To remain
compatible with systems that were installed on older Linux or z/VM
levels, the -u option will print the uid excluding any z/VM-provided
levels, the \-u option will print the uid excluding any z/VM-provided
minidisk token.
For example, if the extended uid is
@@ -57,35 +57,35 @@ uid is IBM.75000000092461.e900.10. If the extended uid contains no
minidisk token, e.g. in an LPAR environment, then both uids are the
same.
.TP
.BI "-l|--label"
.BI "\-l|\-\-label"
Print DASD volume label (volser).
.TP
.BI "-i|--busid " <busid>
.BI "\-i|\-\-busid " <busid>
Use the bus ID as input parameter, e.g. 0.0.e910.
.TP
.BI "-b|--block " <blockdev>
.BI "\-b|\-\-block " <blockdev>
Use the block device name as input parameter, e.g. dasdb.
.TP
.BI "-d|--devnode " <devnode>
.BI "\-d|\-\-devnode " <devnode>
Use a device node as input parameter, e.g. /dev/dasdb.
.TP
.BI "-e|--export"
.BI "\-e|\-\-export"
Print all values (ID_BUS, ID_TYPE, ID_SERIAL).
.TP
.BI "-h|--help"
.BI "\-h|\-\-help"
Print usage text.
.TP
.BI "-v|--version"
.BI "\-v|\-\-version"
Print version number.
.SH EXAMPLES
dasdinfo -u -i 0.0.e910
dasdinfo \-u \-i 0.0.e910
dasdinfo -u -b dasdb
dasdinfo \-u \-b dasdb
dasdinfo -u -d /dev/dasdb
dasdinfo \-u \-d /dev/dasdb
All three examples should return the same unique ID for
All three examples should return the same unique ID for
the same DASD device, e.g. IBM.75000000092461.e900.10.
In case this uid is not available, dasdinfo will return

View File

@@ -7,21 +7,21 @@
dasdview \- Display DASD and VTOC information and dump the content of a DASD
to the console.
.SH SYNOPSIS
\fBdasdview\fR [-h] [-v]
\fBdasdview\fR [\-h] [\-v]
.br
[-b \fIbegin\fR] [-s \fIsize\fR] [-1|-2]
[\-b \fIbegin\fR] [\-s \fIsize\fR] [\-1|\-2]
.br
[-i] [-x] [-j] [-c]
[\-i] [\-x] [\-j] [\-c]
.br
[-l] [-t {\fIinfo\fR|\fIf1\fR|\fIf3\fR|\fIf4\fR|\fIf5\fR|\fIf7\fR|\fIf8\fR|\fIf9\fR}]
[\-l] [\-t {\fIinfo\fR|\fIf1\fR|\fIf3\fR|\fIf4\fR|\fIf5\fR|\fIf7\fR|\fIf8\fR|\fIf9\fR}]
.br
\fIdevice\fR
.SH DESCRIPTION
\fBdasdview\fR prints you some useful information of your disks to the console.
You can display a disk dump by specifying start point and offset and you can
You can display a disk dump by specifying start point and offset and you can
print the volume label and VTOC entries.
The \fIdevice\fR is the node of the device (e.g. '/dev/dasda').
Any device node created by udev for kernel 2.6 can be used
Any device node created by udev for kernel 2.6 can be used
(e.g. '/dev/dasd/0.0.b100/disc').
DASD devices in raw_track_access mode are supported and detected
@@ -29,30 +29,30 @@ automatically. When in raw_track_access mode, the same basic
functions are available as in the regular mode, but the output may
have a slightly different layout:
.IP \(bu 2
The disk dump functions (\fB-b\fR and \fB-s\fR) print the count,
The disk dump functions (\fB\-b\fR and \fB\-s\fR) print the count,
key and data information for the whole track, and not just the
contents of the data areas.
.IP \(bu 2
The VTOC listing (\fB-t\fR) print all specified DSCBs in the same
The VTOC listing (\fB\-t\fR) print all specified DSCBs in the same
format as in the regular mode, but in the sequence as they appear in
the VTOC. The \fB-t info\fR overview contains more details for each
the VTOC. The \fB\-t info\fR overview contains more details for each
data set than in the regular mode, to support the larger variety of
data set layouts.
.SH OPTIONS
.TP
\fB-h\fR or \fB--help\fR
\fB\-h\fR or \fB\-\-help\fR
Print usage and exit.
.TP
\fB-v\fR or \fB--version\fR
\fB\-v\fR or \fB\-\-version\fR
Print version number and exit.
.TP
\fB-b\fR \fIbegin\fR or \fB--begin=\fR\fIbegin\fR
Print a disk dump to the console, starting with \fIbegin\fR. The content of
the disk will be displayed in hexadecimal numbers, ASCII text and EBCDIC text.
If no size is specified dasdview will take the default size. The variable
\fB\-b\fR \fIbegin\fR or \fB\-\-begin=\fR\fIbegin\fR
Print a disk dump to the console, starting with \fIbegin\fR. The content of
the disk will be displayed in hexadecimal numbers, ASCII text and EBCDIC text.
If no size is specified dasdview will take the default size. The variable
\fIbegin\fR can be specified in one of the following ways:
.br
@@ -63,10 +63,10 @@ The default for \fIbegin\fR is \fI0\fR.
.br
\fBNote 1:\fR dasdview will show you the content of your disk using the DASD
driver. If this driver decides to hide or add some parts of the disk, you have
to live with it. This happens for example with the first two tracks of a
cdl-formatted disk. In this case the DASD driver fills up shorter blocks with
zeros to have a constant blocksize. And all applications, including dasdview,
driver. If this driver decides to hide or add some parts of the disk, you have
to live with it. This happens for example with the first two tracks of a
cdl-formatted disk. In this case the DASD driver fills up shorter blocks with
zeros to have a constant blocksize. And all applications, including dasdview,
believe it.
.br
\fBNote 2:\fR In raw_track_access mode \fIbegin\fR must be aligned to
@@ -76,24 +76,24 @@ cylinder as starting point.
examples:
.br
-b 32 --> start printing at Byte 32
\-b 32 --> start printing at Byte 32
.br
-b 32k --> start printing at kByte 32
\-b 32k --> start printing at kByte 32
.br
-b 32m --> start printing at MByte 32
\-b 32m --> start printing at MByte 32
.br
-b 32b --> start printing at block 32
\-b 32b --> start printing at block 32
.br
-b 32t --> start printing at track 32
\-b 32t --> start printing at track 32
.br
-b 32c --> start printing at cylinder 32
\-b 32c --> start printing at cylinder 32
.TP
\fB-s\fR \fIsize\fR or \fB--size=\fR\fIsize\fR
Print a disk dump to the console, starting with \fIbegin\fR, specified with
the \fB-b\fR option and size \fIsize\fR. The content of the disk will be
displayed in hexadecimal numbers, ASCII text and EBCDIC text. If no start
value is specified dasdview will take the default start value. The variable
\fB\-s\fR \fIsize\fR or \fB\-\-size=\fR\fIsize\fR
Print a disk dump to the console, starting with \fIbegin\fR, specified with
the \fB\-b\fR option and size \fIsize\fR. The content of the disk will be
displayed in hexadecimal numbers, ASCII text and EBCDIC text. If no start
value is specified dasdview will take the default start value. The variable
\fIsize\fR can be specified in one of the following ways:
.br
@@ -111,76 +111,76 @@ in raw_track_access mode.
examples:
.br
-s 16 --> use a 16 Byte size
\-s 16 --> use a 16 Byte size
.br
-s 16k --> use a 16 kByte size
\-s 16k --> use a 16 kByte size
.br
-s 16m --> use a 16 MByte size
\-s 16m --> use a 16 MByte size
.br
-s 16b --> use a 16 block size
\-s 16b --> use a 16 block size
.br
-s 16t --> use a 16 track size
\-s 16t --> use a 16 track size
.br
-s 16c --> use a 16 cylinder size
\-s 16c --> use a 16 cylinder size
.TP
\fB-1\fR
This option tells dasdview to print the disk dump using format 1. This means
you will get 16 Bytes per line in hex, ascii and ebcdic. There is no line
\fB\-1\fR
This option tells dasdview to print the disk dump using format 1. This means
you will get 16 Bytes per line in hex, ascii and ebcdic. There is no line
number.
.br
The \fB-1\fR option makes only sense with the \fB-b\fR and/or the \fB-s\fR
options.
The \fB\-1\fR option makes only sense with the \fB\-b\fR and/or the \fB\-s\fR
options.
.br
This is the default.
.TP
\fB-2\fR
This option tells dasdview to print the disk dump using format 2. This means
you will get 8 Bytes per line in hex, ascii and ebcdic. And in addition a line
\fB\-2\fR
This option tells dasdview to print the disk dump using format 2. This means
you will get 8 Bytes per line in hex, ascii and ebcdic. And in addition a line
number and a decimal and hexadecimal byte count will be printed.
.br
The \fB-2\fR option makes only sense with the \fB-b\fR and/or the \fB-s\fR
The \fB\-2\fR option makes only sense with the \fB\-b\fR and/or the \fB\-s\fR
options. In raw_track_access mode this format is not supported and the
option will be ignored.
.TP
\fB-i\fR or \fB--info\fR
\fB\-i\fR or \fB\-\-info\fR
Print some useful information (e.g. device node/number/type or geometry data).
When running dasdview on a kernel 2.6 based distribution the busid
When running dasdview on a kernel 2.6 based distribution the busid
is printed instead of the device number.
.TP
\fB-x\fR or \fB--extended\fR
\fB\-x\fR or \fB\-\-extended\fR
Print some more DASD information (e.g. open count, subchannel identifier).
.TP
\fB-j\fR or \fB--volser\fR
\fB\-j\fR or \fB\-\-volser\fR
Print volume serial number (volume identifier).
.TP
\fB-l\fR or \fB--label\fR
\fB\-l\fR or \fB\-\-label\fR
Print the volume label.
.TP
\fB-c\fR or \fB--characteristic\fR
\fB\-c\fR or \fB\-\-characteristic\fR
Print some information about the device e.g. if it is encrypted.
.TP
\fB-t\fR \fIspec\fR or \fB--vtoc=\fR\fIspec\fR
\fB\-t\fR \fIspec\fR or \fB\-\-vtoc=\fR\fIspec\fR
Print the VTOC (table of content) or single VTOC entries to the console.
\fIspec\fR can be one of the following strings:
.br
\fIinfo\fR:
\fIinfo\fR:
.br
Gives you a VTOC overview. You will see what other S/390 or zSeries operating
Gives you a VTOC overview. You will see what other S/390 or zSeries operating
systems would see (e.g. data set names and sizes).
.br
\fIf1\fR:
.br
Print the content of all format 1 DSCBs.
Print the content of all format 1 DSCBs.
.br
\fIf3\fR:
@@ -188,17 +188,17 @@ Print the content of all format 1 DSCBs.
Print the content of all format 3 DSCBs.
.br
\fIf4\fR:
\fIf4\fR:
.br
Print the content of the format 4 DSCB.
.br
\fIf5\fR:
\fIf5\fR:
.br
Print the content of the format 5 DSCB.
.br
\fIf7\fR:
\fIf7\fR:
.br
Print the content of the format 7 DSCB.
.br
@@ -213,6 +213,6 @@ Print the content of all format 8 DSCBs.
Print the content of all format 9 DSCBs.
.br
\fIall\fR:
\fIall\fR:
.br
Print the content of all DSCBs.
Print the content of all DSCBs.

View File

@@ -28,6 +28,15 @@
# DEVICE=0.0.4e13
# DELAY_MINUTES=5
#
# Dump on ECKD device (DASD)
#
#ON_PANIC=dump
#DUMP_TYPE=eckd
#DEVICE=0.0.1004
#BOOTPROG=0
#BR_CHR=auto
#
# Dump on fcp device (SCSI Disk)
#

View File

@@ -8,16 +8,16 @@ fdasd \- partitioning tool.
.SH SYNOPSIS
interactive mode:
.br
\fBfdasd\fR [-s] [-r] [-C] \fIdevice\fR
\fBfdasd\fR [\-s] [\-r] [\-C] \fIdevice\fR
.br
command line mode:
.br
\fBfdasd\fR [-s] [-r] [-C] {-a[-k|-l \fIvolser\fR]|-i|-p|-c \fIconf_file\fR}
\fBfdasd\fR [\-s] [\-r] [\-C] {\-a[\-k|\-l \fIvolser\fR]|\-i|\-p|\-c \fIconf_file\fR}
[-f \fI[type,blocksize]\fR] \fIdevice\fR
.br
help:
.br
\fBfdasd\fR {-h|-v}
\fBfdasd\fR {\-h|\-v}
.SH DESCRIPTION
\fBfdasd\fR writes a partition table to a cdl (compatible disk layout)
formatted DASD, in the form of
@@ -30,35 +30,35 @@ will use it, otherwise it asks to write a new one.
\fBfdasd\fR can result in loss of data.
.SH OPTIONS
.TP
\fB-h\fR or \fB--help\fR
\fB\-h\fR or \fB\-\-help\fR
Print usage information, then exit.
.TP
\fB-v\fR or \fB--version\fR
\fB\-v\fR or \fB\-\-version\fR
Print version information, then exit.
.TP
\fB-s\fR or \fB--silent\fR
\fB\-s\fR or \fB\-\-silent\fR
Suppress messages in non-interactive mode.
.TP
\fB-r\fR or \fB--verbose\fR
\fB\-r\fR or \fB\-\-verbose\fR
Provide more verbose output.
.TP
\fB-a\fR or \fB--auto\fR
\fB\-a\fR or \fB\-\-auto\fR
Automatically create a partition using the entire disk in non-interactive
mode.
.TP
\fB-k\fR or \fB--keep_volser\fR
\fB\-k\fR or \fB\-\-keep_volser\fR
Keeps the Volume Serial Number when writing the Volume Label.
.br
This is useful if the volume already has a Serial Number that should not be
overwritten. This option is only applicable in non-interactive mode.
.TP
\fB-l\fR \fIvolser\fR or \fB--label\fR \fIvolser\fR
\fB\-l\fR \fIvolser\fR or \fB\-\-label\fR \fIvolser\fR
Specify the volume serial.
.br
\fIvolser\fR is interpreted as ASCII string and is automatically converted to
@@ -82,14 +82,14 @@ quotes. In addition there is a special handling for the '$' sign.
Please specify it using '\\$' if necessary.
.br
e.g. -l 'a@b\\$c#' to get A@B$C#
e.g. \-l 'a@b\\$c#' to get A@B$C#
.br
Omitting this parameter causes fdasd to ask for it in case it is needed.
.br
.TP
\fB-c\fR \fIconf_file\fR or \fB--config\fR \fIconf_file\fR
\fB\-c\fR \fIconf_file\fR or \fB\-\-config\fR \fIconf_file\fR
Use this option to create multiple partitions according to
specifications in a configuration file, \fIconf_file\fR.
.br
@@ -124,37 +124,37 @@ partitions that use the entire disk:
.br
.TP
\fB-i\fR or \fB--volser\fR
\fB\-i\fR or \fB\-\-volser\fR
Print the volume serial, then exit.
.TP
\fB-p\fR or \fB--table\fR
\fB\-p\fR or \fB\-\-table\fR
Print partition table, then exit.
.br
In combination with the -s option fdasd will display a short version of the
In combination with the \-s option fdasd will display a short version of the
partition table.
.TP
\fB-C\fR or \fB--check_host_count\fR
\fB\-C\fR or \fB\-\-check_host_count\fR
Force fdasd to check the host access open count to ensure the device
is not online on another operating system instance
.TP
\fB-f\fR \fI[type,blocksize]\fR or \fB--force\fR \fI[type,blocksize]\fR
\fB\-f\fR \fI[type,blocksize]\fR or \fB\-\-force\fR \fI[type,blocksize]\fR
Force fdasd to work on non DASD devices.
.br
If fdasd is to be used on a block device that is neither a native DASD
nor exposes the proper disk geometry of a DASD of type 3390,
then the --force option can be used to assume the geometry of a
then the \-\-force option can be used to assume the geometry of a
given device type. The default device type is 3390 and the default
block size is 4096. An optional argument of <device type>,<blocksize>
can be used to specify type and blocksize explicitly. For example:
-f
\-f
has the same effect as
-f3390,4096 or --force=3390,4096
\-f3390,4096 or \-\-force=3390,4096
Valid device types are: 3390, 3380, 9345
.br

View File

@@ -3,7 +3,7 @@ include ../common.mak
.DEFAULT_GOAL := all
PKGDATADIR := "$(DESTDIR)$(TOOLS_DATADIR)/genprotimg"
PKGDATADIR := "$(TOOLS_DATADIR)/genprotimg"
TESTS :=
SUBDIRS := boot src man
RECURSIVE_TARGETS := all-recursive install-recursive clean-recursive
@@ -11,8 +11,8 @@ RECURSIVE_TARGETS := all-recursive install-recursive clean-recursive
all: all-recursive
install: install-recursive
$(INSTALL) -d -m 755 "$(PKGDATADIR)"
$(INSTALL) -g $(GROUP) -o $(OWNER) -m 755 samples/check_hostkeydoc "$(PKGDATADIR)"
$(INSTALL) -d -m 755 "$(DESTDIR)$(PKGDATADIR)"
$(INSTALL) -g $(GROUP) -o $(OWNER) -m 755 samples/check_hostkeydoc "$(DESTDIR)$(PKGDATADIR)"
clean: clean-recursive

View File

@@ -7,7 +7,7 @@ DEBUG_FILES := $(addsuffix .debug,$(FILES))
ifeq ($(HOST_ARCH),s390x)
ZIPL_DIR := $(rootdir)/zipl
ZIPL_BOOT_DIR := $(ZIPL_DIR)/boot
PKGDATADIR := $(DESTDIR)$(TOOLS_DATADIR)/genprotimg
PKGDATADIR := $(TOOLS_DATADIR)/genprotimg
INCLUDE_PATHS := $(ZIPL_BOOT_DIR) $(ZIPL_DIR)/include $(rootdir)/include
INCLUDE_PARMS := $(addprefix -I,$(INCLUDE_PATHS))
@@ -86,9 +86,9 @@ stage3b.elf: head.o $(ZIPL_OBJS)
@chmod a-x $@
install: stage3a.bin stage3b_reloc.bin
$(INSTALL) -d -m 755 "$(PKGDATADIR)"
$(INSTALL) -g $(GROUP) -o $(OWNER) -m 644 stage3a.bin "$(PKGDATADIR)"
$(INSTALL) -g $(GROUP) -o $(OWNER) -m 644 stage3b_reloc.bin "$(PKGDATADIR)"
$(INSTALL) -d -m 755 "$(DESTDIR)$(PKGDATADIR)"
$(INSTALL) -g $(GROUP) -o $(OWNER) -m 644 stage3a.bin "$(DESTDIR)$(PKGDATADIR)"
$(INSTALL) -g $(GROUP) -o $(OWNER) -m 644 stage3b_reloc.bin "$(DESTDIR)$(PKGDATADIR)"
else
# Don't generate the dependency files (see `common.mak` for the

View File

@@ -4,7 +4,7 @@
#
# Sample script to verify that a host key document is genuine by
# verifying the issuer, the validity date and the signature.
# Optionally verify the full trust chain using a CA certficate.
# Optionally verify the full trust chain using a CA certificate.
#
# Sample invocation:
#
@@ -15,31 +15,33 @@
# s390-tools is free software; you can redistribute it and/or modify
# it under the terms of the MIT license. See LICENSE for details.
# Allocate temporary files
ISSUER_PUBKEY_FILE=$(mktemp)
SIGNATURE_FILE=$(mktemp)
BODY_FILE=$(mktemp)
ISSUER_DN_FILE=$(mktemp)
SUBJECT_DN_FILE=$(mktemp)
DEF_ISSUER_DN_FILE=$(mktemp)
DEF_ISSUER_ARMONK_DN_FILE=$(mktemp)
DEF_ISSUER_POUGHKEEPSIE_DN_FILE=$(mktemp)
CANONICAL_ISSUER_DN_FILE=$(mktemp)
CRL_SERIAL_FILE=$(mktemp)
# Cleanup on exit
cleanup()
{
rm -f $ISSUER_PUBKEY_FILE $SIGNATURE_FILE $BODY_FILE \
$ISSUER_DN_FILE $SUBJECT_DN_FILE $DEF_ISSUER_DN_FILE \
$CANONICAL_ISSUER_DN_FILE $CRL_SERIAL_FILE
rm -f "$ISSUER_PUBKEY_FILE" "$SIGNATURE_FILE" "$BODY_FILE" \
"$ISSUER_DN_FILE" "$SUBJECT_DN_FILE" "$DEF_ISSUER_ARMONK_DN_FILE" "$DEF_ISSUER_POUGHKEEPSIE_DN_FILE" \
"$CANONICAL_ISSUER_DN_FILE" "$CRL_SERIAL_FILE"
}
trap cleanup EXIT
# Enhanced error checking for bash
if [ -n "${BASH}" ]
then
if [ -n "${BASH}" ]; then
# shellcheck disable=SC3040
set -o posix
# shellcheck disable=SC3040
set -o pipefail
# shellcheck disable=SC3040
set -o nounset
fi
set -e
@@ -47,8 +49,8 @@ set -e
# Usage
usage()
{
cat <<-EOF
Usage: `basename $1` [-d] [-c CA-cert] [-r CRL] host-key-doc signing-key-cert
cat <<-EOF
Usage: $(basename "$1") [-d] [-c CA-cert] [-r CRL] host-key-doc signing-key-cert
Verify an IBM Secure Execution host key document against
a signing key.
@@ -71,8 +73,7 @@ check_verify_chain()
{
# Verify certificate chain in case a CA certificate file/bundle
# was specified on the command line.
if [ $# = 1 ]
then
if [ -z "$2" ]; then
cat >&2 <<-EOF
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
No CA certificate specified! Skipping trust chain verification.
@@ -80,37 +81,37 @@ Make sure that '$1' is a valid certificate.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
EOF
else
openssl verify -crl_download -crl_check $2 &&
openssl verify -crl_download -crl_check -untrusted $2 $1 ||
exit 1
openssl verify -crl_download -crl_check "$2" &&
openssl verify -crl_download -crl_check -untrusted "$2" "$1" ||
exit 1
fi
}
extract_pubkey()
{
openssl x509 -in $1 -pubkey -noout > $2
openssl x509 -in "$1" -pubkey -noout >"$2"
}
extract_signature()
{
# Assuming that the last field is the signature
SIGOFFSET=$(openssl asn1parse -in $1 | tail -1 | cut -d : -f 1)
SIGOFFSET=$(openssl asn1parse -in "$1" | tail -1 | cut -d : -f 1)
openssl asn1parse -in $1 -out $2 -strparse $SIGOFFSET -noout
openssl asn1parse -in "$1" -out "$2" -strparse "$SIGOFFSET" -noout
}
extract_body()
{
# Assuming that the first field is the full cert body
SIGOFFSET=$(openssl asn1parse -in $1 | head -2 | tail -1 | cut -d : -f 1)
SIGOFFSET=$(openssl asn1parse -in "$1" | head -2 | tail -1 | cut -d : -f 1)
openssl asn1parse -in $1 -out $2 -strparse $SIGOFFSET -noout
openssl asn1parse -in "$1" -out "$2" -strparse "$SIGOFFSET" -noout
}
verify_signature()
{
# Assuming that the signature algorithm is SHA512 with RSA
openssl sha512 -verify $1 -signature $2 $3
openssl sha512 -verify "$1" -signature "$2" "$3"
}
canonical_dn()
@@ -120,18 +121,30 @@ canonical_dn()
DNTYPE=$3
OUTPUT=$4
openssl $OBJTYPE -in $OBJ -$DNTYPE -noout -nameopt multiline \
| sort | grep -v $DNTYPE= > $OUTPUT
openssl "$OBJTYPE" -in "$OBJ" -"$DNTYPE" -noout -nameopt multiline |
LC_ALL=C sort | grep -v "$DNTYPE"= >"$OUTPUT"
}
default_issuer()
default_issuer_armonk()
{
cat <<-EOF
commonName = International Business Machines Corporation
countryName = US
localityName = Armonk
organizationName = International Business Machines Corporation
organizationalUnitName = Key Signing Service
stateOrProvinceName = New York
EOF
}
default_issuer_pougkeepsie()
{
cat <<-EOF
commonName = International Business Machines Corporation
countryName = US
localityName = Poughkeepsie
organizationalUnitName = Key Signing Service
organizationName = International Business Machines Corporation
organizationalUnitName = Key Signing Service
stateOrProvinceName = New York
EOF
}
@@ -141,42 +154,37 @@ EOF
# stripping off the prefix
verify_default_issuer()
{
default_issuer > $DEF_ISSUER_DN_FILE
default_issuer_pougkeepsie >"$DEF_ISSUER_POUGHKEEPSIE_DN_FILE"
default_issuer_armonk >"$DEF_ISSUER_ARMONK_DN_FILE"
sed "s/\(^[ ]*organizationalUnitName[ ]*=[ ]*\).*\(Key Signing Service$\)/\1\2/" \
$ISSUER_DN_FILE > $CANONICAL_ISSUER_DN_FILE
"$ISSUER_DN_FILE" >"$CANONICAL_ISSUER_DN_FILE"
if ! diff $CANONICAL_ISSUER_DN_FILE $DEF_ISSUER_DN_FILE
then
if ! {
diff "$CANONICAL_ISSUER_DN_FILE" "$DEF_ISSUER_POUGHKEEPSIE_DN_FILE" ||
diff "$CANONICAL_ISSUER_DN_FILE" "$DEF_ISSUER_ARMONK_DN_FILE"
} >/dev/null 2>&1; then
echo Incorrect default issuer >&2 && exit 1
fi
}
verify_issuer_files()
{
if [ $1 -eq 1 ]
then
verify_default_issuer
fi
if diff $ISSUER_DN_FILE $SUBJECT_DN_FILE
then
echo Issuer verification OK
else
echo Issuer verification failed >&2 && exit 1
if [ "$1" -eq 1 ]; then
verify_default_issuer
fi
}
cert_time()
{
DATE=$(openssl x509 -in $1 -$2 -noout | sed "s/^.*=//")
DATE=$(openssl x509 -in "$1" -"$2" -noout | sed "s/^.*=//")
date -d "$DATE" +%s
}
crl_time()
{
DATE=$(openssl crl -in $1 -$2 -noout | sed "s/^.*=//")
DATE=$(openssl crl -in "$1" -"$2" -noout | sed "s/^.*=//")
date -d "$DATE" +%s
}
@@ -188,8 +196,7 @@ verify_dates()
MSG="${3:-Certificate}"
NOW=$(date +%s)
if [ $START -le $NOW -a $NOW -le $END ]
then
if [ "$START" -le "$NOW" ] && [ "$NOW" -le "$END" ]; then
echo "${MSG} dates are OK"
else
echo "${MSG} date verification failed" >&2 && exit 1
@@ -198,22 +205,21 @@ verify_dates()
crl_serials()
{
openssl crl -in $1 -text -noout | \
grep "Serial Number" > $CRL_SERIAL_FILE
openssl crl -in "$1" -text -noout |
grep "Serial Number" >"$CRL_SERIAL_FILE"
}
check_serial()
{
CERT_SERIAL=$(openssl x509 -in $1 -noout -serial | cut -d = -f 2)
CERT_SERIAL=$(openssl x509 -in "$1" -noout -serial | cut -d = -f 2)
grep -q $CERT_SERIAL $CRL_SERIAL_FILE
grep -q "$CERT_SERIAL" "$CRL_SERIAL_FILE"
}
check_file()
{
[ $# = 0 ] ||
[ -e "$1" ] ||
(echo "File '$1' not found" >&2 && exit 1)
(echo "File '$1' not found" >&2 && exit 1)
}
# check args
@@ -221,28 +227,25 @@ CRL_FILE=
CA_FILE=
CHECK_DEFAULT_ISSUER=1
args=$(getopt -qu "dr:c:h" $*)
if [ $? = 0 ]
then
set -- $args
while [ $1 != "" ]
do
case $1 in
-d) CHECK_DEFAULT_ISSUER=0; shift;;
-r) CRL_FILE=$2; shift 2;;
-c) CA_FILE=$2; shift 2;;
-h) usage $0; exit 0;;
--) shift; break;;
esac
done
else
usage $0 >&2
exit 1
fi
while getopts 'dr:c:h' opt; do
case $opt in
d) CHECK_DEFAULT_ISSUER=0 ;;
r) CRL_FILE=$OPTARG ;;
c) CA_FILE=$OPTARG ;;
h)
usage "$0"
exit 0
;;
?)
usage "$0"
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
if [ $# -ne 2 ]
then
usage $0 >&2
if [ $# -ne 2 ]; then
usage "$0" >&2
exit 1
fi
@@ -250,51 +253,51 @@ HKD_FILE=$1
HKSK_FILE=$2
# Check whether all specified files exist
check_file $HKD_FILE
check_file $HKSK_FILE
check_file $CA_FILE
check_file $CRL_FILE
check_file "$HKD_FILE"
check_file "$HKSK_FILE"
# CA and CRL are optional arguments
[ -n "$CA_FILE" ] && check_file "$CA_FILE"
[ -n "$CRL_FILE" ] && check_file "$CRL_FILE"
# Check trust chain
check_verify_chain $HKSK_FILE $CA_FILE
check_verify_chain "$HKSK_FILE" "$CA_FILE"
# Verify host key document signature
echo -n "Checking host key document signature: "
extract_pubkey $HKSK_FILE $ISSUER_PUBKEY_FILE &&
extract_signature $HKD_FILE $SIGNATURE_FILE &&
extract_body $HKD_FILE $BODY_FILE &&
verify_signature $ISSUER_PUBKEY_FILE $SIGNATURE_FILE $BODY_FILE ||
exit 1
printf "Checking host key document signature: "
extract_pubkey "$HKSK_FILE" "$ISSUER_PUBKEY_FILE" &&
extract_signature "$HKD_FILE" "$SIGNATURE_FILE" &&
extract_body "$HKD_FILE" "$BODY_FILE" &&
verify_signature "$ISSUER_PUBKEY_FILE" "$SIGNATURE_FILE" "$BODY_FILE" ||
exit 1
# Verify the issuer
canonical_dn x509 $HKD_FILE issuer $ISSUER_DN_FILE
canonical_dn x509 $HKSK_FILE subject $SUBJECT_DN_FILE
canonical_dn x509 "$HKD_FILE" issuer "$ISSUER_DN_FILE"
canonical_dn x509 "$HKSK_FILE" subject "$SUBJECT_DN_FILE"
verify_issuer_files $CHECK_DEFAULT_ISSUER
# Verify dates
verify_dates $(cert_time $HKD_FILE startdate) $(cert_time $HKD_FILE enddate)
verify_dates "$(cert_time "$HKD_FILE" startdate)" "$(cert_time "$HKD_FILE" enddate)"
# Check CRL if specified
if [ -n "$CRL_FILE" ]
then
echo -n "Checking CRL signature: "
extract_signature $CRL_FILE $SIGNATURE_FILE &&
extract_body $CRL_FILE $BODY_FILE &&
verify_signature $ISSUER_PUBKEY_FILE $SIGNATURE_FILE $BODY_FILE ||
exit 1
if [ -n "$CRL_FILE" ]; then
printf "Checking CRL signature: "
extract_signature "$CRL_FILE" "$SIGNATURE_FILE" &&
extract_body "$CRL_FILE" "$BODY_FILE" &&
verify_signature "$ISSUER_PUBKEY_FILE" "$SIGNATURE_FILE" "$BODY_FILE" ||
exit 1
echo -n "CRL "
canonical_dn crl $CRL_FILE issuer $ISSUER_DN_FILE
canonical_dn x509 $HKSK_FILE subject $SUBJECT_DN_FILE
printf "CRL "
canonical_dn crl "$CRL_FILE" issuer "$ISSUER_DN_FILE"
canonical_dn x509 "$HKSK_FILE" subject "$SUBJECT_DN_FILE"
verify_issuer_files $CHECK_DEFAULT_ISSUER
verify_dates $(crl_time $CRL_FILE lastupdate) $(crl_time $CRL_FILE nextupdate) 'CRL'
verify_dates "$(crl_time "$CRL_FILE" lastupdate)" "$(crl_time "$CRL_FILE" nextupdate)" 'CRL'
crl_serials $CRL_FILE
check_serial $HKD_FILE &&
echo "Certificate is revoked, do not use it anymore!" >&2 &&
exit 1
crl_serials "$CRL_FILE"
check_serial "$HKD_FILE" &&
echo "Certificate is revoked, do not use it anymore!" >&2 &&
exit 1
fi
# We made it
echo All checks reqested for \'$HKD_FILE\' were successful
echo All checks requested for \'"$HKD_FILE"\' were successful

View File

@@ -3,7 +3,7 @@ include ../../common.mak
bin_PROGRAM = genprotimg
PKGDATADIR ?= "$(DESTDIR)$(TOOLS_DATADIR)/genprotimg"
PKGDATADIR ?= "$(TOOLS_DATADIR)/genprotimg"
SRC_DIR := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
TOP_SRCDIR := $(SRC_DIR)/../
ROOT_DIR = $(TOP_SRC_DIR)/../../

View File

@@ -17,7 +17,8 @@
/* IBM signing key subject */
#define PV_IBM_Z_SUBJECT_COMMON_NAME "International Business Machines Corporation"
#define PV_IBM_Z_SUBJECT_COUNTRY_NAME "US"
#define PV_IBM_Z_SUBJECT_LOCALITY_NAME "Poughkeepsie"
#define PV_IBM_Z_SUBJECT_LOCALITY_NAME_POUGHKEEPSIE "Poughkeepsie"
#define PV_IBM_Z_SUBJECT_LOCALITY_NAME_ARMONK "Armonk"
#define PV_IBM_Z_SUBJECT_ORGANIZATIONONAL_UNIT_NAME_SUFFIX "Key Signing Service"
#define PV_IBM_Z_SUBJECT_ORGANIZATION_NAME "International Business Machines Corporation"
#define PV_IBM_Z_SUBJECT_STATE "New York"

View File

@@ -664,62 +664,9 @@ static gboolean x509_name_data_by_nid_equal(X509_NAME *name, gint nid,
return memcmp(data, y, data_len) == 0;
}
static gboolean own_X509_NAME_ENTRY_equal(const X509_NAME_ENTRY *x,
const X509_NAME_ENTRY *y)
{
const ASN1_OBJECT *x_obj = X509_NAME_ENTRY_get_object(x);
const ASN1_STRING *x_data = X509_NAME_ENTRY_get_data(x);
const ASN1_OBJECT *y_obj = X509_NAME_ENTRY_get_object(y);
const ASN1_STRING *y_data = X509_NAME_ENTRY_get_data(y);
gint x_len = ASN1_STRING_length(x_data);
gint y_len = ASN1_STRING_length(y_data);
if (x_len < 0 || x_len != y_len)
return FALSE;
/* ASN1_STRING_cmp(x_data, y_data) == 0 doesn't work because it also
* compares the type, which is sometimes different.
*/
return OBJ_cmp(x_obj, y_obj) == 0 &&
memcmp(ASN1_STRING_get0_data(x_data),
ASN1_STRING_get0_data(y_data),
(unsigned long)x_len) == 0;
}
static gboolean own_X509_NAME_equal(const X509_NAME *x, const X509_NAME *y)
{
gint x_count = X509_NAME_entry_count(x);
gint y_count = X509_NAME_entry_count(y);
if (x != y && (!x || !y))
return FALSE;
if (x_count != y_count)
return FALSE;
for (gint i = 0; i < x_count; i++) {
const X509_NAME_ENTRY *entry_i = X509_NAME_get_entry(x, i);
gboolean entry_found = FALSE;
for (gint j = 0; j < y_count; j++) {
const X509_NAME_ENTRY *entry_j =
X509_NAME_get_entry(y, j);
if (own_X509_NAME_ENTRY_equal(entry_i, entry_j)) {
entry_found = TRUE;
break;
}
}
if (!entry_found)
return FALSE;
}
return TRUE;
}
/* Checks whether the subject of @cert is a IBM signing key subject. For this we
* must check that the subject is equal to: 'C = US, ST = New York, L =
* Poughkeepsie, O = International Business Machines Corporation, CN =
* Poughkeepsie or Armonk, O = International Business Machines Corporation, CN =
* International Business Machines Corporation' and the organization unit (OUT)
* must end with the suffix ' Key Signing Service'.
*/
@@ -743,8 +690,10 @@ static gboolean has_ibm_signing_subject(X509 *cert)
PV_IBM_Z_SUBJECT_STATE))
return FALSE;
if (!x509_name_data_by_nid_equal(subject, NID_localityName,
PV_IBM_Z_SUBJECT_LOCALITY_NAME))
if (!(x509_name_data_by_nid_equal(subject, NID_localityName,
PV_IBM_Z_SUBJECT_LOCALITY_NAME_POUGHKEEPSIE) ||
x509_name_data_by_nid_equal(subject, NID_localityName,
PV_IBM_Z_SUBJECT_LOCALITY_NAME_ARMONK)))
return FALSE;
if (!x509_name_data_by_nid_equal(subject, NID_organizationName,
@@ -806,6 +755,39 @@ static X509_NAME *x509_name_reorder_attributes(const X509_NAME *name, const gint
return g_steal_pointer(&ret);
}
/** Replace locality 'Armonk' with 'Pougkeepsie'. If Armonk was not set return
* `NULL`.
*/
static X509_NAME *x509_armonk_locality_fixup(const X509_NAME *name)
{
g_autoptr(X509_NAME) ret = NULL;
int pos;
/* Check if ``L=Armonk`` */
if (!x509_name_data_by_nid_equal((X509_NAME *)name, NID_localityName,
PV_IBM_Z_SUBJECT_LOCALITY_NAME_ARMONK))
return NULL;
ret = X509_NAME_dup((X509_NAME *)name);
if (!ret)
g_abort();
pos = X509_NAME_get_index_by_NID(ret, NID_localityName, -1);
if (pos == -1)
return NULL;
X509_NAME_ENTRY_free(X509_NAME_delete_entry(ret, pos));
/* Create a new name entry at the same position as before */
if (X509_NAME_add_entry_by_NID(
ret, NID_localityName, MBSTRING_UTF8,
(const unsigned char *)&PV_IBM_Z_SUBJECT_LOCALITY_NAME_POUGHKEEPSIE,
sizeof(PV_IBM_Z_SUBJECT_LOCALITY_NAME_POUGHKEEPSIE) - 1, pos, 0) != 1)
return NULL;
return g_steal_pointer(&ret);
}
/* In RFC 5280 the attributes of a (subject/issuer) name is not mandatory
* ordered. The problem is that our certificates are not consistent in the order
* (see https://tools.ietf.org/html/rfc5280#section-4.1.2.4 for details).
@@ -828,24 +810,10 @@ X509_NAME *c2b_name(const X509_NAME *name)
return X509_NAME_dup((X509_NAME *)name);
}
/* Verify that: subject(issuer) == issuer(crl) and SKID(issuer) == AKID(crl) */
/* Verify that SKID(issuer) == AKID(crl) if available */
static gint check_crl_issuer(X509_CRL *crl, X509 *issuer, GError **err)
{
const X509_NAME *crl_issuer = X509_CRL_get_issuer(crl);
const X509_NAME *issuer_subject = X509_get_subject_name(issuer);
AUTHORITY_KEYID *akid = NULL;
if (!own_X509_NAME_equal(issuer_subject, crl_issuer)) {
g_autofree char *issuer_subject_str = X509_NAME_oneline(issuer_subject,
NULL, 0);
g_autofree char *crl_issuer_str = X509_NAME_oneline(crl_issuer, NULL, 0);
g_set_error(err, PV_CRYPTO_ERROR,
PV_CRYPTO_ERROR_CRL_SUBJECT_ISSUER_MISMATCH,
_("issuer mismatch:\n%s\n%s"),
issuer_subject_str, crl_issuer_str);
return -1;
}
g_autoptr(AUTHORITY_KEYID) akid = NULL;
/* If AKID(@crl) is specified it must match with SKID(@issuer) */
akid = X509_CRL_get_ext_d2i(crl, NID_authority_key_identifier, NULL, NULL);
@@ -881,7 +849,6 @@ gint check_crl_valid_for_cert(X509_CRL *crl, X509 *cert,
return -1;
}
/* check that the @crl issuer matches with the subject name of @cert*/
if (check_crl_issuer(crl, cert, err) < 0)
return -1;
@@ -910,6 +877,60 @@ gint check_crl_valid_for_cert(X509_CRL *crl, X509 *cert,
return 0;
}
/* This function contains work-arounds for some known subject(CRT)<->issuer(CRL)
* issues.
*/
static STACK_OF_X509_CRL *quirk_X509_STORE_ctx_get1_crls(X509_STORE_CTX *ctx,
const X509_NAME *subject, GError **err)
{
g_autoptr(X509_NAME) fixed_subject = NULL;
g_autoptr(STACK_OF_X509_CRL) ret = NULL;
ret = Pv_X509_STORE_CTX_get1_crls(ctx, subject);
if (ret && sk_X509_CRL_num(ret) > 0)
return g_steal_pointer(&ret);
/* Workaround to fix the mismatch between issuer name of the * IBM
* signing CRLs and the IBM signing key subject name. Locality name has
* changed from Poughkeepsie to Armonk.
*/
fixed_subject = x509_armonk_locality_fixup(subject);
/* Was the locality replaced? */
if (fixed_subject) {
X509_NAME *tmp;
sk_X509_CRL_free(ret);
ret = Pv_X509_STORE_CTX_get1_crls(ctx, fixed_subject);
if (ret && sk_X509_CRL_num(ret) > 0)
return g_steal_pointer(&ret);
/* Workaround to fix the ordering mismatch between issuer name
* of the IBM signing CRLs and the IBM signing key subject name.
*/
tmp = fixed_subject;
fixed_subject = c2b_name(fixed_subject);
X509_NAME_free(tmp);
sk_X509_CRL_free(ret);
ret = Pv_X509_STORE_CTX_get1_crls(ctx, fixed_subject);
if (ret && sk_X509_CRL_num(ret) > 0)
return g_steal_pointer(&ret);
X509_NAME_free(fixed_subject);
fixed_subject = NULL;
}
/* Workaround to fix the ordering mismatch between issuer name of the
* IBM signing CRLs and the IBM signing key subject name.
*/
fixed_subject = c2b_name(subject);
sk_X509_CRL_free(ret);
ret = Pv_X509_STORE_CTX_get1_crls(ctx, fixed_subject);
if (ret && sk_X509_CRL_num(ret) > 0)
return g_steal_pointer(&ret);
g_set_error(err, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_NO_CRL, _("no CRL found"));
return NULL;
}
/* Given a certificate @cert try to find valid revocation lists in @ctx. If no
* valid CRL was found NULL is returned.
*/
@@ -927,20 +948,9 @@ STACK_OF_X509_CRL *store_ctx_find_valid_crls(X509_STORE_CTX *ctx, X509 *cert,
return NULL;
}
ret = X509_STORE_CTX_get1_crls(ctx, subject);
if (!ret) {
/* Workaround to fix the mismatch between issuer name of the
* IBM Z signing CRLs and the IBM Z signing key subject name.
*/
g_autoptr(X509_NAME) broken_subject = c2b_name(subject);
ret = X509_STORE_CTX_get1_crls(ctx, broken_subject);
if (!ret) {
g_set_error(err, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_NO_CRL,
_("no CRL found"));
return NULL;
}
}
ret = quirk_X509_STORE_ctx_get1_crls(ctx, subject, err);
if (!ret)
return NULL;
/* Filter out non-valid CRLs for @cert */
for (gint i = 0; i < sk_X509_CRL_num(ret); i++) {
@@ -1328,32 +1338,14 @@ gint check_chain_parameters(const STACK_OF_X509 *chain,
/* It's almost the same as X509_check_issed from OpenSSL does except that we
* don't check the key usage of the potential issuer. This means we check:
* 1. issuer_name(cert) == subject_name(issuer)
* 2. Check whether the akid(cert) (if available) matches the issuer skid
* 3. Check that the cert algrithm matches the subject algorithm
* 4. Verify the signature of certificate @cert is using the public key of
* 1. Check whether the akid(cert) (if available) matches the issuer skid
* 2. Check that the cert algrithm matches the subject algorithm
* 3. Verify the signature of certificate @cert is using the public key of
* @issuer.
*/
static gint check_host_key_issued(X509 *cert, X509 *issuer, GError **err)
{
const X509_NAME *issuer_subject = X509_get_subject_name(issuer);
const X509_NAME *cert_issuer = X509_get_issuer_name(cert);
AUTHORITY_KEYID *akid = NULL;
/* We cannot use X509_NAME_cmp() because it considers the order of the
* X509_NAME_Entries.
*/
if (!own_X509_NAME_equal(issuer_subject, cert_issuer)) {
g_autofree char *issuer_subject_str =
X509_NAME_oneline(issuer_subject, NULL, 0);
g_autofree char *cert_issuer_str =
X509_NAME_oneline(cert_issuer, NULL, 0);
g_set_error(err, PV_CRYPTO_ERROR,
PV_CRYPTO_ERROR_CERT_SUBJECT_ISSUER_MISMATCH,
_("Subject issuer mismatch:\n'%s'\n'%s'"),
issuer_subject_str, cert_issuer_str);
return -1;
}
g_autoptr(AUTHORITY_KEYID) akid = NULL;
akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
if (akid && X509_check_akid(issuer, akid) != X509_V_OK) {
@@ -1834,14 +1826,12 @@ static gint __encrypt_decrypt_bio(const struct cipher_parms *parms, BIO *b_in,
g_assert(out_len >= 0);
num_bytes_written = BIO_write(b_out, out_buf, out_len);
if (num_bytes_written < 0) {
if (num_bytes_written != out_len) {
g_set_error(err, PV_CRYPTO_ERROR,
PV_CRYPTO_ERROR_INTERNAL,
_("Failed to write"));
return -1;
}
g_assert(num_bytes_written == out_len);
tmp_size_out += (guint)num_bytes_written;
/* Set new tweak value. Please keep in mind that the

View File

@@ -75,6 +75,7 @@ void x509_pair_free(x509_pair *pair);
/* Register auto cleanup functions */
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(ASN1_INTEGER, ASN1_INTEGER_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(ASN1_OCTET_STRING, ASN1_OCTET_STRING_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(AUTHORITY_KEYID, AUTHORITY_KEYID_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(BIGNUM, BN_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(BIO, BIO_free_all)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(BN_CTX, BN_CTX_free)

View File

@@ -24,11 +24,25 @@
#define OS_INFO_VMCOREINFO 0
#define OS_INFO_REIPL_BLOCK 1
#define OS_INFO_FLAGS_ENTRY 2
#define OS_INFO_RESERVED 3
#define OS_INFO_IDENTITY_BASE 4
#define OS_INFO_KASLR_OFFSET 5
#define OS_INFO_KASLR_OFF_PHYS 6
#define OS_INFO_VMEMMAP 7
#define OS_INFO_AMODE31_START 8
#define OS_INFO_AMODE31_END 9
#define OS_INFO_IMAGE_START 10
#define OS_INFO_IMAGE_END 11
#define OS_INFO_IMAGE_PHYS 12
#define OS_INFO_MAX 13
#define OS_INFO_FLAG_REIPL_CLEAR (1UL << 0)
struct os_info_entry {
uint64_t addr;
union {
uint64_t addr;
uint64_t val;
};
uint64_t size;
uint32_t csum;
} __packed;
@@ -40,8 +54,8 @@ struct os_info {
uint16_t version_minor;
uint64_t crashkernel_addr;
uint64_t crashkernel_size;
struct os_info_entry entry[3];
uint8_t reserved[4004];
struct os_info_entry entry[OS_INFO_MAX];
uint8_t reserved[3804];
} __packed;
/*

View File

@@ -26,6 +26,17 @@
#define DF_S390_EM_STR "DUMP_END"
#define DF_S390_CPU_MAX 512
#define DF_S390_MAGIC_BLK_ECKD 3
#define DF_S390_DUMPER_MAGIC_SIZE 7
#define DF_S390_DUMPER_MAGIC32 "ZECKD31"
#define DF_S390_DUMPER_MAGIC64 "ZECKD64"
#define DF_S390_DUMPER_MAGIC_EXT "XECKD64"
#define DF_S390_DUMPER_MAGIC32_FBA "ZDFBA31"
#define DF_S390_DUMPER_MAGIC64_FBA "ZDFBA64"
#define DF_S390_DUMPER_MAGIC_FBA_EXT "XDFBA64"
#define DF_S390_DUMPER_MAGIC_MV "ZMULT64"
#define DF_S390_DUMPER_MAGIC_MV_EXT "XMULT64"
#define OLD_DUMPER_HEX_INSTR1 "\x0d\x10\x47\xf0" /* BASR + 1st halfword of BC */
#define OLD_DUMPER_HEX_INSTR2 "\x0d\xd0" /* BASR 13,0 */
/*
* Architecture of dumped system

View File

@@ -21,7 +21,9 @@
#define AP_UDEV_FILE "/etc/udev/rules.d/41-ap.rules"
#define AP_LOCKFILE "/run/lock/s390apconfig.lock"
#define AP_LOCK_RETRIES 15
#define AP_LOCK_RETRIES 3000
#define AP_LOCK_DELAY_US 30000 /* wait at least 30ms between lock retries */
#define AP_LOCK_VARIANCE_US 3000 /* or as much as 33ms */
/* apmask and aqmask are each represented as 67 character strings with:
* '0x' leading characters

View File

@@ -27,6 +27,7 @@ enum util_arch_machine_type {
UTIL_ARCH_MACHINE_TYPE_Z15 = 8561,
UTIL_ARCH_MACHINE_TYPE_Z15_T02 = 8562,
UTIL_ARCH_MACHINE_TYPE_Z16 = 3931,
UTIL_ARCH_MACHINE_TYPE_Z16_A02 = 3932,
};
int util_arch_machine_type(void);

View File

@@ -14,7 +14,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zt_common.h"
#include "lib/util_libc.h"
void util_hexdump(FILE *fh, const char *tag, const void *data, int cnt);
void util_hexdump_grp(FILE *fh, const char *tag, const void *data, int group,
@@ -37,4 +40,30 @@ static inline void util_ptr_vec_free(void **ptr_vec, int count)
free(ptr_vec);
}
/*
* Expand size of dynamic array (element_t *) by one element
*
* @param[in,out] array Pointer to array (element_t **)
* @param[in,out] num Pointer to integer containing number of elements
*/
#define util_expand_array(array, num) \
do { \
unsigned int __size = sizeof(*(*(array))); \
*(array) = util_realloc(*(array), ++(*(num)) * __size); \
memset(&((*(array))[*(num) - 1]), 0, __size); \
} while (0)
/*
* Append one element to dynamic array (element_t *)
*
* @param[in,out] array Pointer to array (element_t **)
* @param[in,out] num Pointer to integer containing number of elements
* @param[in] element Element to add (element_t)
*/
#define util_add_array(array, num, element) \
do { \
util_expand_array(array, num); \
(*(array))[*(num) - 1] = (element) ; \
} while (0)
#endif /* LIB_UTIL_BASE_H */

235
include/lib/util_fmt.h Normal file
View File

@@ -0,0 +1,235 @@
/*
* util_fmt - Format structured key-value data as JSON, text pairs, or CSV
*
* Copyright IBM Corp. 2024
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*
* This module provides helper functions for converting structured key-value
* data into different output formats.
*
* Benefits:
* - Output format can be dynamically configured at run-time
* - Callers do not need to add extra code for each output format
* - Some format-specific requirements such as quoting, indentation, and
* comma-placement are automated
*
* Basic API calling sequence:
*
* util_fmt_init() => Select output format
* util_fmt_obj_start() => Start a new object or list
* util_fmt_pair() => Emit a key-value pair
* util_fmt_obj_end() => End the latest object or list
* util_fmt_exit() => Cleanup
*
* Note:
* - Supported data elements are objects, lists and key-value pairs (mappings)
* - Scalars are only supported as part of a mapping
* - For CSV output and key filtering, mapping keys must be unique - this can
* be achieved either by choosing unique key names or by including object
* names via the FMT_PREFIX flag
* - For CSV output, at least one object or list with the FMT_ROW flag must be
* emitted
* - Common tool-specific meta-information such as API-level, tool version,
* etc. is automatically added to the output
*/
#ifndef LIB_UTIL_FMT_H
#define LIB_UTIL_FMT_H
#include <stdbool.h>
#include <stdio.h>
/* Flag value for default behavior (all flag types). */
#define FMT_DEFAULT 0
/* Names of supported output format types. */
#define FMT_TYPE_NAMES "json json-seq pairs csv"
/**
* enum util_fmt_t - Output format types.
* @FMT_JSON: JavaScript Object Notation output data structure
* @FMT_JSONSEQ: Sequence of JSON data structures according to RFC7464
* @FMT_PAIRS: Textual key=value pairs
* @FMT_CSV: Comma-separated-values output
*
* Use these types with util_fmt_init() to control the output format.
*/
enum util_fmt_t {
FMT_JSON,
FMT_JSONSEQ,
FMT_PAIRS,
FMT_CSV,
};
/**
* enum util_fmt_flags_t - Format control flags.
* @FMT_NOPREFIX: (pairs) Remove object hierarchy prefix from keys
* @FMT_KEEPINVAL: (all) Print mappings even if value is marked as invalid
* Values will be replaced with null (JSON) or an empty
* string
* @FMT_QUOTEALL: (all) Add quotes to all mapping values
* @FMT_FILTER: (all) Ignore keys not announced via util_fmt_add_key()
* @FMT_HANDLEINT: (json) Ensure correct JSON closure when interrupted
* @FMT_NOMETA: (all) Do not emit tool meta-data
* @FMT_WARN: (all) Warn about incorrect API usage
*
* Use these flags with util_fmt_init() to control generic aspects.
*/
enum util_fmt_flags_t {
FMT_NOPREFIX = (1 << 0),
FMT_KEEPINVAL = (1 << 1),
FMT_QUOTEALL = (1 << 2),
FMT_FILTER = (1 << 3),
FMT_HANDLEINT = (1 << 4),
FMT_NOMETA = (1 << 5),
FMT_WARN = (1 << 6),
};
/**
* enum util_fmt_oflags_t - Object flags.
* @FMT_LIST: (all) Object is a list
* @FMT_ROW: (csv) Start a new CSV row with this object
* @FMT_PREFIX: (all) Include object name in key prefix for CSV headings
* and filter keys
*
* Use these flags with util_fmt_obj_start() to control object related
* aspects.
*/
enum util_fmt_oflags_t {
FMT_LIST = (1 << 0),
FMT_ROW = (1 << 1),
FMT_PREFIX = (1 << 2),
};
/**
* enum util_fmt_mflags_t - Mapping flags.
* @FMT_QUOTE: (all) Quote value
* @FMT_INVAL: (all) Mark value as invalid
* @FMT_PERSIST: (csv) Keep value across CSV rows until overwritten
*
* Use these flags with util_fmt_pair() to control mapping related aspects.
*/
enum util_fmt_mflags_t {
FMT_QUOTE = (1 << 0),
FMT_INVAL = (1 << 1),
FMT_PERSIST = (1 << 2),
};
/**
* util_fmt_init() - Initialize output formatter.
* @fd : Output file descriptor
* @type : Output format type
* @flags: Formatting parameters
* @api_level: Output format level indicator
*
* Prepare for writing formatted output with the given @type to @fd. Additional
* @flags can be specified to control certain output aspects (see &enum
* util_fmt_flags_t).
*
* @api_level represents an application-specific output format version number:
* this number starts at 1 and must be increased whenever an incompatible format
* change is introduced, e.g. when a non-optional object or mapping is removed
* or used for different data.
*/
void util_fmt_init(FILE *fd, enum util_fmt_t type, unsigned int flags,
int api_level);
/**
* util_fmt_exit() - Release resources used by output formatter.
*
* Release all resources currently in use by the output formatter.
*/
void util_fmt_exit(void);
/**
* util_fmt_name_to_type() - Convert format name to type identifier.
* @name: Format name
* @type: Pointer to resulting format type identifier
*
* Search supported output format types for a type with associated @name. If
* found, store resulting type identifier in @type.
*
* Return: %true if type is found, %false otherwise.
*/
bool util_fmt_name_to_type(const char *name, enum util_fmt_t *type);
/**
* util_fmt_set_indent() - Set indentation parameters.
* @base : Base indentation level to apply to all output lines (default 0)
* @width : Number of indentation characters per intendation level (default 2)
* @ind_char: Indentation characters to use (default space).
*/
void util_fmt_set_indent(unsigned int base, unsigned int width, char ind_char);
/**
* util_fmt_add_key() - Register expected mapping keys.
* @fmt: Format string to generate key
*
* Register a mapping key before the associated key-value pair is emitted.
*
* Use this function together with format control flag @FMT_FILTER to ignore all
* key-value pairs for which the key has not been registered. This can be
* useful to allow for dynamically configured filtering of output based on
* a static list of emitted mappings.
*
* When creating CSV output, use this function to register all column keys
* in advance to enable a stable column list in case of rows that do not
* provide data for all columns.
*/
void util_fmt_add_key(const char *fmt, ...);
/**
* util_fmt_obj_start() - Start a new data object.
* @oflags: Flags controlling aspects of this object.
* @fmt : Format string for generating an object name or %NULL.
*
* Use this function to start a new object in output data. Depending on
* @oflags, the new object represents either a normal object or a list. @oflags
* can also be used to indicated that an object corresponds to a new row of
* CSV data. If @fmt is non-%NULL, the resulting name is used in a format
* type specified way:
*
* Pairs:
* - Object names are reflected as dot-separated component in the mapping
* prefix, e.g. 'a.b.key=value'
* - An index is generated for mappings and objects that are part of list,
* e.g. 'a.b[1].key=value'
* JSON:
* - Object names are reflected as key-object mappings, e.g.
* <name>: { }
* - Required commas between objects and mappings are automatically generated
* CSV:
* - Object names and the list type flag have no effect
* - When flag @FMT_ROW is specified, a CSV row will be emitted when
* util_fmt_obj_end() is called for the associated object
*/
void util_fmt_obj_start(unsigned int oflags, const char *fmt, ...);
/**
* util_fmt_obj_end() - Announce the end of the latest data object started.
*
* Each object started with util_fmt_obj_start() must be ended with an
* associated util_fmt_obj_end() call.
*/
void util_fmt_obj_end(void);
/**
* util_fmt_pair() - Emit a key-value pair.
* @mflags: Flags controlling this pair.
* @key : Key for this pair, excluding prefix.
* @fmt : Format string used to generated the pair value.
*
* Emit a key-value pair with the specified @key and the value that results
* from format string @fmt.
*
* Notes:
* - For JSON, a mapping can only occur after util_fmt_obj_start()
* - For CSV, each @key must be unique, either by choosing unique key names
* or by including object names as prefix via the use of FMT_PREFIX in
* parent objects
*/
void util_fmt_pair(unsigned int mflags, const char *key, const char *fmt, ...);
#endif /* LIB_UTIL_FMT_H */

View File

@@ -127,6 +127,7 @@ do { \
int __util_vsprintf(const char *func, const char *file, int line,
char *str, const char *fmt, va_list ap);
char *util_strcat_realloc(char *str1, const char *str2);
void util_concatf(char **str1, const char *fmt, ...);
void util_str_toupper(char *str);
char *util_strstrip(char *s);

View File

@@ -18,7 +18,11 @@
#define UTIL_LOCKFILE_ERR 4 /* Other, unexpected error conditions */
int util_lockfile_lock(char *lockfile, int retries);
int util_lockfile_lock_cw(char *lockfile, int retries, unsigned int waitinc,
unsigned int maxwait);
int util_lockfile_parent_lock(char *lockfile, int retries);
int util_lockfile_parent_lock_cw(char *lockfile, int retries,
unsigned int waitinc, unsigned int maxwait);
int util_lockfile_release(char *lockfile);
int util_lockfile_parent_release(char *lockfile);

View File

@@ -40,6 +40,7 @@
#define LV_COMPAT_CYL 0xFFFE
#define VTOC_ERROR "VTOC error:"
#define MAX_VTOC_ENTRIES 9 /* max number of VTOC labels for cdl formatted DASD */
typedef struct ttr
{

View File

@@ -1,439 +0,0 @@
/*
* Certificate functions and definitions.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef LIBPV_CERT_H
#define LIBPV_CERT_H
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include "libpv/common.h"
#define PV_IBM_Z_SUBJECT_COMMON_NAME "International Business Machines Corporation"
#define PV_IBM_Z_SUBJECT_COUNTRY_NAME "US"
#define PV_IBM_Z_SUBJECT_LOCALITY_NAME "Poughkeepsie"
#define PV_IBM_Z_SUBJECT_ORGANIZATIONAL_UNIT_NAME_SUFFIX "Key Signing Service"
#define PV_IBM_Z_SUBJECT_ORGANIZATION_NAME "International Business Machines Corporation"
#define PV_IBM_Z_SUBJECT_STATE "New York"
#define PV_IMB_Z_SUBJECT_ENTRY_COUNT 6
/* Minimum security level for the keys/certificates used to establish a chain of
* trust (see https://www.openssl.org/docs/man1.1.1/man3/X509_VERIFY_PARAM_set_auth_level.html
* for details).
*/
#define PV_CERTS_SECURITY_LEVEL 2
/** pv_cert_init:
*
* Should not be called by user.
* Use pv_init() instead which
* calls this function during creation.
*
* Sets up data structures for caching CRLs.
*/
void pv_cert_init(void);
/** pv_cert_cleanup:
*
* Should not be called by user.
* Use pv_cleanup() instead which
* calls this function during creation.
*
* Cleans up data structures for caching CRLs.
*/
void pv_cert_cleanup(void);
#define PV_CERT_ERROR g_quark_from_static_string("pv-cert-error-quark")
typedef enum {
PV_CERT_ERROR_CERT_REVOKED,
PV_CERT_ERROR_CERT_SIGNATURE_INVALID,
PV_CERT_ERROR_CERT_SUBJECT_ISSUER_MISMATCH,
PV_CERT_ERROR_CRL_DOWNLOAD_FAILED,
PV_CERT_ERROR_CRL_SIGNATURE_INVALID,
PV_CERT_ERROR_CRL_SUBJECT_ISSUER_MISMATCH,
PV_CERT_ERROR_FAILED_DOWNLOAD_CRL,
PV_CERT_ERROR_INTERNAL,
PV_CERT_ERROR_INVALID_PARM,
PV_CERT_ERROR_INVALID_SIGNATURE_ALGORITHM,
PV_CERT_ERROR_INVALID_VALIDITY_PERIOD,
PV_CERT_ERROR_LOAD_CRL,
PV_CERT_ERROR_LOAD_DEFAULT_CA,
PV_CERT_ERROR_LOAD_ROOT_CA,
PV_CERT_ERROR_MALFORMED_CERTIFICATE,
PV_CERT_ERROR_MALFORMED_ROOT_CA,
PV_CERT_ERROR_NO_CRL,
PV_CERT_ERROR_NO_CRLDP,
PV_CERT_ERROR_NO_IBM_Z_SIGNING_KEY,
PV_CERT_ERROR_NO_ISSUER_IBM_Z_FOUND,
PV_CERT_ERROR_NO_PUBLIC_KEY,
PV_CERT_ERROR_READ_CERTIFICATE,
PV_CERT_ERROR_READ_CRL,
PV_CERT_ERROR_SIGNATURE_ALGORITHM_MISMATCH,
PV_CERT_ERROR_SKID_AKID_MISMATCH,
PV_CERT_ERROR_VERIFICATION_FAILED,
PV_CERT_ERROR_WRONG_CA_USED,
} PvCertErrors;
/** PvX509WithPath - X509 certificate associated with a path
*/
typedef struct {
X509 *cert;
char *path;
} PvX509WithPath;
/** pv_x509_with_path_new:
*
* @cert: X509 certificate
* @path: Path of that X509 certificate
*
* Returns: (nullable) (transfer full): new X509 with path
*/
PvX509WithPath *pv_x509_with_path_new(X509 *cert, const char *path);
/** pv_x509_with_path_free:
*
* Frees the path and the PvX509WithPath; Decreases the refcount of the X509
*/
void pv_x509_with_path_free(PvX509WithPath *cert);
typedef STACK_OF(DIST_POINT) STACK_OF_DIST_POINT;
typedef STACK_OF(X509) STACK_OF_X509;
typedef STACK_OF(X509_CRL) STACK_OF_X509_CRL;
typedef GSList PvCertWithPathList;
typedef struct {
X509 *cert;
STACK_OF_X509_CRL *crls;
} PvX509Pair;
/** pv_x509_pair_new_take:
* @cert: ptr to X509
* @crls: ptr to CRLs
*
* Takes a X509 and the associated CRLs and builds a pair.
* Both, *cert and *crls will be NULL afterwards, and owned by the pair.
*
* Returns: (nullable) (transfer full): New PvX509Pair
*/
PvX509Pair *pv_x509_pair_new_take(X509 **cert, STACK_OF_X509_CRL **crls);
/** pv_x509_pair_free:
*
* Decreases the refcount of the X509 and crls.
* Frees the PvX509Pair.
*/
void pv_x509_pair_free(PvX509Pair *pair);
void STACK_OF_DIST_POINT_free(STACK_OF_DIST_POINT *stack);
void STACK_OF_X509_free(STACK_OF_X509 *stack);
void STACK_OF_X509_CRL_free(STACK_OF_X509_CRL *stack);
/** pv_x509_from_pem_der_data:
*
* @data: GBytes containing the cert in PEM format
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): X509 cert
*/
X509 *pv_x509_from_pem_der_data(GBytes *data, GError **error);
/** pv_x509_get_ec_pubkey:
*
* @cert: X509 to extract elliptic curve pubkey from
* @nid: numerical identifier of the expected curve
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): corresponding pupkey for the given certificate
*/
EVP_PKEY *pv_x509_get_ec_pubkey(X509 *cert, int nid, GError **error);
/** pv_get_ec_pubkeys:
*
* @certs_with_path: List of PvX509WithPath
* @nid: numerical identifier of the expected curve
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): List of corresponding public keys for the given certificate
*/
GSList *pv_get_ec_pubkeys(PvCertWithPathList *certs_with_path, int nid, GError **error);
/* pv_load_certificates:
*
* @cert_paths: list of cert paths.
* @error: return location for a #GError
*
* @cert_paths must contain at least one element, otherwise an error is
* reported.
*
* Returns: (nullable) (transfer full): List of PvX509WithPath corresponding to the given paths
*/
PvCertWithPathList *pv_load_certificates(char **cert_paths, GError **error);
/* pv_load_first_cert_from_file:
*
* @path: location of the x509
* @error: return location for a #GError
*
* This function reads in only the first certificate and ignores all other. This
* is only relevant for the PEM file format. For the host-key document and the
* root CA this behavior is expected.
*
* Returns: (nullable) (transfer full): PvX509WithPath corresponding to the given path
*/
X509 *pv_load_first_cert_from_file(const char *path, GError **error);
/* pv_load_first_crl_from_file:
*
* @path: location of the x509 CRL
* @error: return location for a #GError
*
* This function reads in only the first CRL and ignores all other. This
* is only relevant for the PEM file format.
*
* Returns: (nullable) (transfer full): X509_CRL corresponding to the given path
*/
X509_CRL *pv_load_first_crl_from_file(const char *path, GError **error);
/** pv_store_setup_crl_download:
*
* @st: X509_STORE
*/
void pv_store_setup_crl_download(X509_STORE *st);
/** pv_load_first_crl_by_cert:
* @cert: X509 to specify the download location.
* @error: return location for a #GError
*
* This function returns the first X509_CRL found from the CRL distribution
* points specified in @cert.
*
* Returns: (nullable) (transfer full): x509 CRL corresponding to the given X509
*/
X509_CRL *pv_load_first_crl_by_cert(X509 *cert, GError **error);
/** pv_try_load_crls_by_certs:
*
* @certs_with_path: List of PvX509WithPath
*
* Returns: (nullable) (transfer full): Stack of CRLs corresponding to the given X509
*/
STACK_OF_X509_CRL *pv_try_load_crls_by_certs(PvCertWithPathList *certs_with_path);
/** pv_store_setup:
*
* @root_ca_path: Location of the rootCA or NULL if SystemRoot CA shall be used
* @crl_paths: List of CRL paths or NULL
* @cert_with_crl_paths: List of (untrusted) X509 paths
* @error: return location for a #GError
*
* The untrusted certs need to be verified before actually verifying a Host Key Document.
*
* Returns: (nullable) (transfer full): X509_store with given input data.
*
*/
X509_STORE *pv_store_setup(char *root_ca_path, char **crl_paths, char **cert_with_crl_paths,
GError **error);
/** pv_get_x509_stack:
*
* x509_with_path_list: list of PvX509WithPath
*
* Returns: (nullable) (transfer full): Stack of X509 corresponding to the given x509 with path
*/
STACK_OF_X509 *pv_get_x509_stack(const GSList *x509_with_path_list);
/** pv_init_store_ctx:
*
* @ctx: a uninitialized Store CTX
* @trusted: X509_STORE with a trusted rootCA
* @chain: untrusted X509s
* @error: return location for a #GError
*
* Can be called multiple times on the same context if X509_STORE_CTX_cleanup(ctx)
* was called before.
*
* Returns:
* 0 on success
* -1 in failure
*/
int pv_init_store_ctx(X509_STORE_CTX *ctx, X509_STORE *trusted, STACK_OF_X509 *chain,
GError **error) PV_NONNULL(1, 2, 3);
/** pv_init_store_ctx:
*
* @trusted: X509_STORE with a trusted rootCA
* @chain: untrusted X509s
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): X509_STORE_CTX setup with the input data
*/
X509_STORE_CTX *pv_create_store_ctx(X509_STORE *trusted, STACK_OF_X509 *chain, GError **error)
PV_NONNULL(1, 2);
/** pv_remove_ibm_signing_certs:
*
* @certs: Stack of X509s
*
* Returns: (transfer full):
* List of all IBM Z signing key certificates in @certs and remove them
* from the chain.
* Empty stack if no IBM Z signing key is found.
*/
STACK_OF_X509 *pv_remove_ibm_signing_certs(STACK_OF_X509 *certs);
/** pv_c2b_name:
*
* Workaround to fix the mismatch between issuer name of the
* IBM Z signing CRLs and the IBM Z signing key subject name.
*
* In RFC 5280 the attributes of a (subject/issuer) name is not mandatory
* ordered. The problem is that our certificates are not consistent in the order
* (see https://tools.ietf.org/html/rfc5280#section-4.1.2.4 for details).
*
* This function tries to reorder the name attributes such that
* further OpenSSL calls can work with it. The caller is
* responsible to free the returned value.
*/
X509_NAME *pv_c2b_name(const X509_NAME *name);
/** pv_verify_host_key:
*
* @host_key: X509 to be verified
* @issuer_pairs: IBM signing key X509+CRLs Pairs used for verification
* @level: Security level. see PV_CERTS_SECURITY_LEVEL
* @error: return location for a #GError
*
* Returns:
* 0 if Host key could be verified with one of the IBM signing keys
* -1 if no IBM signing key could verify the authenticity of the given host key
*
*/
int pv_verify_host_key(X509 *host_key, GSList *issuer_pairs, int verify_flags, int level,
GError **error);
/** pv_verify_cert:
*
* @ctx: trusted store ctx used for verification
* @cert: X509 to be verified
* @error: return location for a #GError
*
* Cannot be used to verify host keys with IBM signing keys, as IBM signing
* keys are no intermediate CAs. Use pv_verify_host_key() instead.
*
* Returns:
* 0 if @cert could be verified
* -1 if @cert could not be verified
*/
int pv_verify_cert(X509_STORE_CTX *ctx, X509 *cert, GError **error) PV_NONNULL(1, 2);
/** pv_check_crl_valid_for_cert:
*
* @crl: CRL to be verified
* @cert: Cert that probably issued the given CRL
* @verify_flags: X509 Verification flags (X509_V_FLAG_<TYPE>)
* @error: return location for a #GError
*
* Verify whether a revocation list @crl is valid and is issued by @cert. For
* this multiple steps must be done:
*
* 1. verify issuer of the CRL matches with the suject name of @cert
* 2. verify the validity period of the CRL
* 3. verify the signature of the CRL
*
* Important: This function does not verify whether @cert is allowed to issue a
* CRL.
*
* Returns:
* 0 if @crl is valid and issued by @cert
* -1 otherwise
*/
int pv_verify_crl(X509_CRL *crl, X509 *cert, int verify_flags, GError **error);
/** pv_check_chain_parameters:
*
* @chain: chain of trust to be validated
* @error: return location for a #GError
*
* Verifies that chain has at least a RootCA ans intermediate CA
* and logs the used ROD CA subject
*
* Returns:
* 0 @chain is valid
* -1 otherwise
*/
int pv_check_chain_parameters(const STACK_OF_X509 *chain, GError **error);
/** pv_store_set_verify_param:
*
* @store: X509_STORE to set parameters
* @error: return location for a #GError
*
* Returns:
* 0 on success
* -1 on failure
*/
int pv_store_set_verify_param(X509_STORE *store, GError **error);
/** pv_store_ctx_find_valid_crls:
*
* @ctx: STORE_CTX for searching CRLs
* @cert: X509 to match CRLs aggainst
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): STACK of CRLs related to given @crl fin @ctx
*/
STACK_OF_X509_CRL *pv_store_ctx_find_valid_crls(X509_STORE_CTX *ctx, X509 *cert, GError **error)
PV_NONNULL(1, 2);
/** pv_verify_host_key_doc:
*
* @host_key_certs_with_path: X509s to be verified
* @trusted. X509_STORE with a rusted RootCA
* @untrusted_certs: STACK OF untrusted X509s
* @online: true if CRLs shall be downloaded
* @error: return location for a #GError
*
* Returns:
* 0 if all given HKDs could be verified using the chain of trust.
* -1 otherwise
*/
int pv_verify_host_key_doc(PvCertWithPathList *host_key_certs_with_path, X509_STORE *trusted,
STACK_OF_X509 *untrusted_certs, gboolean online, GError **error)
PV_NONNULL(1, 2, 3);
/** pv_verify_host_key_docs_by_path:
*
* @host_key_paths: locations of X509 to be verified
* @optional_root_ca_path: rootCA location or NULL if Default shall be used
* @optional_crl_paths: locations of CRLs or NULL
* @untrusted_cert_paths: locations of IntermediateCAs including the IBM signing key
* @online: true if CRLs shall be downloaded
* @error: return location for a #GError
*
* Returns:
* 0 if all given HKDs could be verfied using the chain of trust.
* -1 otherwise
*/
int pv_verify_host_key_docs_by_path(char **host_key_paths, char *optional_root_ca_path,
char **optional_crl_paths, char **untrusted_cert_paths,
gboolean online, GError **error) PV_NONNULL(1, 4);
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(AUTHORITY_KEYID, AUTHORITY_KEYID_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(PvX509WithPath, pv_x509_with_path_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(STACK_OF_DIST_POINT, STACK_OF_DIST_POINT_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(STACK_OF_X509, STACK_OF_X509_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(STACK_OF_X509_CRL, STACK_OF_X509_CRL_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(X509, X509_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(X509_CRL, X509_CRL_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(X509_LOOKUP, X509_LOOKUP_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(X509_NAME, X509_NAME_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(X509_VERIFY_PARAM, X509_VERIFY_PARAM_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(PvX509Pair, pv_x509_pair_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(X509_STORE, X509_STORE_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(X509_STORE_CTX, X509_STORE_CTX_free)
#endif /* LIBPV_CERT_H */

View File

@@ -14,22 +14,6 @@
* the glib version is supported
*/
#include "libpv/glib-helper.h"
#include <glib/gi18n.h>
#include "libpv/openssl-compat.h"
#include "libpv/macros.h"
/** pv_init:
*
* Must be called before any libpv call.
*/
int pv_init(void);
/** pv_cleanup:
*
* Must be called when done with using libpv.
*/
void pv_cleanup(void);
#endif /* LIBPV_COMMON_H */

View File

@@ -15,6 +15,7 @@
#include <openssl/evp.h>
#include "libpv/common.h"
#define PV_NONNULL(...)
typedef struct pv_cipher_parms {
const EVP_CIPHER *cipher;
@@ -26,17 +27,6 @@ typedef struct pv_cipher_parms {
};
} PvCipherParms;
typedef union {
struct {
uint8_t x[80];
uint8_t y[80];
};
uint8_t data[160];
} PvEcdhPubKey;
G_STATIC_ASSERT(sizeof(PvEcdhPubKey) == 160);
typedef GSList PvEvpKeyList;
enum PvCryptoMode {
PV_ENCRYPT,
PV_DECRYPT,
@@ -61,42 +51,6 @@ char *pv_get_openssl_errors(void);
*/
int pv_BIO_reset(BIO *b);
/**
* pv_generate_rand_data:
* @size: number of generated random bytes using a crypographically secure pseudo random generator
* @error: return location for a #GError
*
* Creates a new #GBytes with @size random bytes using a cryptographically
* secure pseudo random generator.
*
* Returns: (nullable) (transfer full): a new #GBytes, or %NULL in case of an error
*/
GBytes *pv_generate_rand_data(size_t size, GError **error);
/**
* pv_generate_key:
* @cipher: specifies the OpenSSL cipher for which a cryptographically secure key should be generated
* @error: return location for a #GError
*
* Creates a random key for @cipher using a cryptographically secure pseudo
* random generator.
*
* Returns: (nullable) (transfer full): a new #GBytes, or %NULL in case of an error
*/
GBytes *pv_generate_key(const EVP_CIPHER *cipher, GError **error) PV_NONNULL(1);
/**
* pv_generate_iv:
* @cipher: specifies the OpenSSL cipher for which a cryptographically secure IV should be generated
* @error: return location for a #GError
*
* Creates a random IV for @cipher using a cryptographically secure pseudo
* random generator.
*
* Returns: (nullable) (transfer full): a new #GBytes, or %NULL in case of an error
*/
GBytes *pv_generate_iv(const EVP_CIPHER *cipher, GError **error) PV_NONNULL(1);
/* Symmetric en/decryption functions */
/**
@@ -135,7 +89,7 @@ int64_t pv_gcm_decrypt(GBytes *cipher, GBytes *aad, GBytes *tag, const PvCipherP
* @derived_key_len: size of the output key
* @key: input key
* @salt: salt for the extraction
* @info: infor for the expansion
* @info: info for the expansion
* @md: EVP mode of operation
* @error: return location for a #GError
*
@@ -147,54 +101,15 @@ int64_t pv_gcm_decrypt(GBytes *cipher, GBytes *aad, GBytes *tag, const PvCipherP
GBytes *pv_hkdf_extract_and_expand(size_t derived_key_len, GBytes *key, GBytes *salt, GBytes *info,
const EVP_MD *md, GError **error) PV_NONNULL(2, 3, 4, 5);
/** pv_generate_ec_key:
*
* @nid: Numerical identifier of the curve
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): new random key based on the given curve
*/
EVP_PKEY *pv_generate_ec_key(int nid, GError **error);
/** pv_evp_pkey_to_ecdh_pub_key:
*
* @key: input key in EVP_PKEY format
* @error: return location for a #GError
*
* Returns: the public part of the input @key in ECDH format.
*/
PvEcdhPubKey *pv_evp_pkey_to_ecdh_pub_key(EVP_PKEY *key, GError **error) PV_NONNULL(1);
/** pv_derive_exchange_key:
* @cust: Customer Key
* @host: Host key
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): Shared Secret of @cust and @host
*/
GBytes *pv_derive_exchange_key(EVP_PKEY *cust, EVP_PKEY *host, GError **error) PV_NONNULL(1, 2);
GQuark pv_crypto_error_quark(void);
#define PV_CRYPTO_ERROR pv_crypto_error_quark()
typedef enum {
PV_CRYPTO_ERROR_DERIVE,
PV_CRYPTO_ERROR_HKDF_FAIL,
PV_CRYPTO_ERROR_INTERNAL,
PV_CRYPTO_ERROR_INVALID_KEY_SIZE,
PV_CRYPTO_ERROR_KEYGENERATION,
PV_CRYPTO_ERROR_RANDOMIZATION,
PV_CRYPTO_ERROR_READ_FILE,
PV_CRYPTO_ERROR_NO_MATCH_TAG,
} PvCryptoErrors;
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(ASN1_INTEGER, ASN1_INTEGER_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(ASN1_OCTET_STRING, ASN1_OCTET_STRING_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(BIO, BIO_free_all)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(BIGNUM, BN_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(BN_CTX, BN_CTX_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(EC_GROUP, EC_GROUP_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(EC_KEY, EC_KEY_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(EC_POINT, EC_POINT_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(EVP_PKEY, EVP_PKEY_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(EVP_PKEY_CTX, EVP_PKEY_CTX_free)

View File

@@ -1,53 +0,0 @@
/*
* Libcurl utils
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef LIBPV_CURL_H
#define LIBPV_CURL_H
#include <curl/curl.h>
#include "libpv/common.h"
#define CRL_DOWNLOAD_TIMEOUT_MS 3000
#define CRL_DOWNLOAD_MAX_SIZE 0x100000
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(CURL, curl_easy_cleanup)
/** curl_download:
* @url: URL to specify location of data
* @timeout_ms: time to wait until fail
* @max_size: Maximum size of the downloaded data
* @error: return location for a GError
*
* Returns: (nullable) (transfer full): Downloaded data as #GByteArray
*/
GByteArray *curl_download(const char *url, long timeout_ms, uint max_size, GError **err);
/** pv_curl_init:
*
* Should not be called by user.
* Use pv_init() instead which
* calls this function during creation.
*/
int pv_curl_init(void);
/** pv_curl_cleanup:
*
* Should not be called by user.
* Use pv_cleanup() instead which
* calls this function during creation.
*/
void pv_curl_cleanup(void);
#define PV_CURL_ERROR g_quark_from_static_string("pv-curl-error-quark")
typedef enum {
PV_CURL_ERROR_CURL_INIT_FAILED,
PV_CURL_ERROR_DOWNLOAD_FAILED,
} PvCurlErrors;
#endif /* LIBPV_CURL_H */

View File

@@ -28,8 +28,6 @@
#include <gmodule.h>
#include <stdio.h>
#include "libpv/macros.h"
#ifdef __clang__
#define WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(...) \
DO_PRAGMA(clang diagnostic push) \
@@ -40,6 +38,8 @@
#define WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(...) G_DEFINE_AUTOPTR_CLEANUP_FUNC(__VA_ARGS__)
#endif
#define DO_PRAGMA(x) _Pragma(#x)
#define pv_wrapped_g_assert(__expr) g_assert(__expr)
/** pv_sec_gbytes_new_take:

View File

@@ -1,119 +0,0 @@
/*
* Hashing definitions.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef LIBPV_HASH_H
#define LIBPV_HASH_H
#include <openssl/hmac.h>
#include "libpv/common.h"
/** pv_digest_ctx_new:
* @md: mode of digest, e.g. #EVP_sha256()
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): a new #EVP_MD_CTX, or %NULL in case of an error
*/
EVP_MD_CTX *pv_digest_ctx_new(const EVP_MD *md, GError **error);
/** pv_digest_ctx_update:
* @ctx: EVP_MD_CTX to add data
* @data: #GBytes to add to the context
* @error: return location for a #GError
*
* Adds @data to the digest context. Can be called multiple times.
*
* Returns: 0 in case of success, -1 otherwise.
*/
int pv_digest_ctx_update(EVP_MD_CTX *ctx, GBytes *data, GError **error);
/** pv_digest_ctx_update_raw:
* @ctx: #EVP_MD_CTX to add data
* @buf: data to add to the context
* @size: size of @buf
* @error: return location for a #GError
*
* Adds @buf to the digest context. Can be called multiple times.
*
* Returns: 0 in case of success, -1 otherwise.
*/
int pv_digest_ctx_update_raw(EVP_MD_CTX *ctx, const uint8_t *buf, size_t size, GError **error);
/** pv_digest_ctx_finalize:
* @ctx: #EVP_MD_CTX with data to digest
* @error: return location for a #GError
*
* Calculates the digest of all previously added data. Do not use @ctx afterwards.
*
* Returns: (nullable) (transfer full): Digest of all data added before as #GBytes, or NULL in case of error.
*/
GBytes *pv_digest_ctx_finalize(EVP_MD_CTX *ctx, GError **error);
/** pv_sha256_hash:
* @buf: data for which a sha256 hash sould be calculated
* @size: size of @buf
* @error: return location for a #GError
*
* Shorthand for initializing a sha256-digest ctx, updating, and finalizing.
*
* Returns: (nullable) (transfer full): SHA256 of @buf as #GBytes, or NULL in case of error.
*/
GBytes *pv_sha256_hash(uint8_t *buf, size_t size, GError **error);
/** pv_hmac_ctx_new:
* @key: key used for the HMAC
* @md: mode of digest, e.g. #EVP_sha512()
* @error: return location for a #GError
*
* Returns: (nullable) (transfer full): New #HMAC_CTX or NULL in case of error
*/
HMAC_CTX *pv_hmac_ctx_new(GBytes *key, const EVP_MD *md, GError **error);
/** pv_hmac_ctx_update_raw:
* @ctx: #HMAC_CTX to add data
* @buf: data to add to the context
* @size: size of @buf
* @error: return location for a #GError
*
* Adds @buf to the HMAC context. Can be called multiple times.
*
* Returns: 0 in case of success, -1 otherwise.
*/
int pv_hmac_ctx_update_raw(HMAC_CTX *ctx, const void *data, size_t size, GError **error);
/** pv_hmac_ctx_update:
* @ctx: #HMAC_CTX to add data
* @data: #GBytes to add to the context
* @error: return location for a #GError
*
* Adds @data to the HMAC context. Can be called multiple times.
*
* Returns: 0 in case of success, -1 otherwise.
*/
int pv_hmac_ctx_update(HMAC_CTX *ctx, GBytes *data, GError **error);
/** pv_hmac_ctx_finalize:
* @ctx: #HMAC_CTX with data to digest
* @error: return location for a #GError
*
* Calculates the HMAC of all previously added data. Do not use @ctx afterwards.
*
* Returns: (nullable) (transfer full): HMAC of all data added before as #GBytes, or NULL in case of error.
*/
GBytes *pv_hamc_ctx_finalize(HMAC_CTX *ctx, GError **error);
#define PV_HASH_ERROR g_quark_from_static_string("pv-crypro-error-quark")
typedef enum {
PV_HASH_ERROR_INTERNAL,
} PvHashErrors;
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(EVP_MD_CTX, EVP_MD_CTX_free)
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(HMAC_CTX, HMAC_CTX_free)
#endif /* LIBPV_HASH_H */

View File

@@ -1,21 +0,0 @@
/*
* Libpv common macro definitions.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*
*/
#ifndef LIBPV_MACROS_H
#define LIBPV_MACROS_H
#include <stdint.h>
#define PV_NONNULL(...)
#define DO_PRAGMA(x) _Pragma(#x)
/* Most significant bit */
#define PV_MSB(idx) ((uint64_t)1 << (63 - (idx)))
#endif /* LIBPV_MACROS_H */

View File

@@ -1,29 +0,0 @@
/*
* OpenSSL compatibility utils
*
* Copyright IBM Corp. 2021
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef LIBPV_OPENSSL_COMPAT_H
#define LIBPV_OPENSSL_COMPAT_H
#include <openssl/opensslv.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#define pv_X509_STORE_CTX_get_current_cert(ctx) X509_STORE_CTX_get_current_cert(ctx)
#define pv_X509_STORE_CTX_get1_crls(ctx, nm) X509_STORE_CTX_get1_crls((ctx), (nm))
#define pv_X509_STORE_set_lookup_crls(st, cb) X509_STORE_set_lookup_crls(st, cb)
#elif OPENSSL_VERSION_NUMBER >= 0x10100000L
#define pv_X509_STORE_CTX_get_current_cert(ctx) \
X509_STORE_CTX_get_current_cert((X509_STORE_CTX *)(ctx))
#define pv_X509_STORE_CTX_get1_crls(ctx, nm) \
X509_STORE_CTX_get1_crls((X509_STORE_CTX *)(ctx), (X509_NAME *)(nm))
#define pv_X509_STORE_set_lookup_crls(st, cb) \
X509_STORE_set_lookup_crls(st, (X509_STORE_CTX_lookup_crls_fn)(cb))
#endif
#endif /* LIBPV_OPENSSL_COMPAT_H */

View File

@@ -1,96 +0,0 @@
/*
* PV/SE header definitions
*
* Copyright IBM Corp. 2020
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef LIBPV_SE_HDR_H
#define LIBPV_SE_HDR_H
#include "libpv/common.h"
#include <openssl/sha.h>
#include "boot/psw.h"
#include "libpv/crypto.h"
#include "libpv/macros.h"
/* Magic number which is used to identify the file containing the PV
* header
*/
#define PV_MAGIC_NUMBER 0x49424d5365634578ULL
#define PV_VERSION_1 0x00000100U
/* Plaintext control flags */
/* dumping of the configuration is allowed */
#define PV_PCF_ALLOW_DUMPING PV_MSB(34)
/* prevent Ultravisor decryption during unpack operation */
#define PV_PCF_NO_DECRYPTION PV_MSB(35)
/* PCKMO encrypt-DEA/TDEA-key functions allowed */
#define PV_PCF_PCKMO_DEA_TDEA PV_MSB(56)
/* PCKMO encrypt-AES-key functions allowed */
#define PV_PCF_PCKMO_AES PV_MSB(57)
/* PCKMO encrypt-ECC-key functions allowed */
#define PV_PCF_PCKM_ECC PV_MSB(58)
/* maxima for the PV version 1 */
#define PV_V1_IPIB_MAX_SIZE PAGE_SIZE
#define PV_V1_PV_HDR_MIN_SIZE \
(sizeof(struct pv_hdr_head) + sizeof(struct pv_hdr_encrypted) + \
sizeof(((struct pv_hdr *)0)->tag) + 1 * sizeof(struct pv_hdr_key_slot))
#define PV_V1_PV_HDR_MAX_SIZE (2 * PAGE_SIZE)
#define PV_IMAGE_ENCR_KEY_SIZE 64
typedef struct pv_hdr_key_slot {
uint8_t digest_key[SHA256_DIGEST_LENGTH];
uint8_t wrapped_key[32];
uint8_t tag[16];
} __packed PvHdrKeySlot;
typedef struct pv_hdr_opt_item {
uint32_t otype;
uint8_t ibk[32];
uint8_t data[];
} __packed PvHdrOptItem;
/* integrity protected data (by GCM tag), but non-encrypted */
struct pv_hdr_head {
uint64_t magic;
uint32_t version;
uint32_t phs;
uint8_t iv[12];
uint32_t res1;
uint64_t nks;
uint64_t sea;
uint64_t nep;
uint64_t pcf;
PvEcdhPubKey cust_pub_key;
uint8_t pld[SHA512_DIGEST_LENGTH];
uint8_t ald[SHA512_DIGEST_LENGTH];
uint8_t tld[SHA512_DIGEST_LENGTH];
} __packed;
/* Must not have any padding */
struct pv_hdr_encrypted {
uint8_t cust_comm_key[32];
uint8_t img_enc_key_1[PV_IMAGE_ENCR_KEY_SIZE / 2];
uint8_t img_enc_key_2[PV_IMAGE_ENCR_KEY_SIZE / 2];
struct psw_t psw;
uint64_t scf;
uint32_t noi;
uint32_t res2;
};
G_STATIC_ASSERT(sizeof(struct pv_hdr_encrypted) == 32 + 32 + 32 + sizeof(struct psw_t) + 8 + 4 + 4);
typedef struct pv_hdr {
struct pv_hdr_head head;
struct pv_hdr_key_slot *slots;
struct pv_hdr_encrypted *encrypted;
struct pv_hdr_opt_item **optional_items;
uint8_t tag[16];
} PvHdr;
#endif /* LIBPV_SE_HDR_H */

View File

@@ -15,8 +15,31 @@
#include <string.h>
#include <unistd.h>
#include "lib/util_path.h"
#include "lib/util_panic.h"
#include "ipl_tools.h"
/*
* Look up for the device in /sys/devices/ hierarchy.
*
* path must be PATH_MAX large and the value will be replaced in place
*/
static int device_sysfs_path(const char *device, char *path, const size_t path_size)
{
util_assert(device != NULL, "Internal error: device is NULL");
util_assert(path != NULL, "Internal error: path is NULL");
util_assert(path_size == PATH_MAX, "Internal error: path_size is '%zu', but must be '%zu'",
path_size, PATH_MAX);
char *buf = util_path_sysfs("block/%s/device", device);
if (!realpath(buf, path)) {
free(buf);
return -1;
}
free(buf);
return 0;
}
/*
* Check if the specified device number is a valid device number
* which can be found in the /sys/bus/ccw/drivers/dasd-eckd/
@@ -42,6 +65,26 @@ int ccw_is_device(const char *busid)
return 0;
}
/*
* Check if the specified device is a valid virtio subchannel device
*/
int ccw_is_virtio_device(const char *device)
{
char path[PATH_MAX] = { '\0' };
unsigned virtio = 0;
if (device_sysfs_path(device, path, sizeof(path)) != 0)
return -1;
/*
* The output has the following format:
* /sys/devices/css0/0.0.0000/0.0.0000/virtio0/block/vda
*/
if (sscanf(path, "/sys/devices/css0/%*[0-9a-f.]/%*[0-9a-f.]/virtio%u", &virtio) != 1)
return -1;
return 0;
}
/*
* Return CCW Bus ID (old sysfs)
*/
@@ -77,11 +120,9 @@ out_fclose:
*/
static int ccw_busid_get_sysfs_new(const char *device, char *busid)
{
char path[PATH_MAX], buf[4096];
char path[PATH_MAX] = { '\0' };
memset(buf, 0, sizeof(buf));
snprintf(path, sizeof(path), "/sys/block/%s/device", device);
if (realpath(path, buf) == NULL)
if (device_sysfs_path(device, path, sizeof(path)) != 0)
return -1;
/*
@@ -89,7 +130,7 @@ static int ccw_busid_get_sysfs_new(const char *device, char *busid)
* /sys/devices/css0/0.0.0119/0.0.3f19/block/dasda
* /sys/devices/css0/0.0.0000/0.0.0000/virtio0/block/vda
*/
if (sscanf(buf, "/sys/devices/css0/%*[0-9a-f.]/%[0-9a-f.]", busid) != 1)
if (sscanf(path, "/sys/devices/css0/%*[0-9a-f.]/%[0-9a-f.]", busid) != 1)
return -1;
return 0;
}

View File

@@ -391,7 +391,8 @@ static void dev_from_part_nvme(char *dev_name)
static int set_reipl_type(const char *dev_name)
{
if (strncmp(dev_name, "dasd", strlen("dasd")) == 0 ||
strncmp(dev_name, "vd", strlen("vd")) == 0)
strncmp(dev_name, "vd", strlen("vd")) == 0 ||
ccw_is_virtio_device(dev_name) == 0)
l.reipl_type = REIPL_CCW;
else if (strncmp(dev_name, "sd", strlen("sd")) == 0)
l.reipl_type = REIPL_FCP;

View File

@@ -87,6 +87,7 @@ int nvme_is_device(char *fid_str, char *nsid_str);
* CCW
*/
extern int ccw_is_device(const char *devno);
extern int ccw_is_virtio_device(const char *device);
extern void ccw_busid_get(const char *device, char *devno);
/*

View File

@@ -11,12 +11,15 @@
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#ifdef HAVE_JSONC
#include <json-c/json.h>
@@ -698,6 +701,20 @@ void ap_list_remove_all(struct util_list *list)
}
}
static unsigned int random_delay(void)
{
static bool libap_seed = true;
struct timeval t;
if (libap_seed) {
gettimeofday(&t, NULL);
srand((unsigned int)((t.tv_sec + t.tv_usec) % UINT_MAX));
libap_seed = false;
}
return AP_LOCK_DELAY_US + (rand() % AP_LOCK_VARIANCE_US);
}
/**
* Acquire the ap config lock using this Process ID
*
@@ -707,7 +724,9 @@ void ap_list_remove_all(struct util_list *list)
*/
int ap_get_lock(void)
{
return util_lockfile_lock(AP_LOCKFILE, AP_LOCK_RETRIES);
unsigned int delay = random_delay();
return util_lockfile_lock_cw(AP_LOCKFILE, AP_LOCK_RETRIES, delay, delay);
}
/**
@@ -719,7 +738,10 @@ int ap_get_lock(void)
*/
int ap_get_lock_callout(void)
{
return util_lockfile_parent_lock(AP_LOCKFILE, AP_LOCK_RETRIES);
unsigned int delay = random_delay();
return util_lockfile_parent_lock_cw(AP_LOCKFILE, AP_LOCK_RETRIES, delay,
delay);
}
/**

View File

@@ -9,10 +9,7 @@ GLIB2_CFLAGS := $(shell $(PKG_CONFIG) --silence-errors --cflags glib-2.0)
GLIB2_LIBS := $(shell $(PKG_CONFIG) --silence-errors --libs glib-2.0)
LIBCRYPTO_CFLAGS := $(shell $(PKG_CONFIG) --silence-errors --cflags libcrypto)
LIBCRYPTO_LIBS := $(shell $(PKG_CONFIG) --silence-errors --libs libcrypto)
LIBCURL_CFLAGS := $(shell $(PKG_CONFIG) --silence-errors --cflags libcurl)
LIBCURL_LIBS := $(shell $(PKG_CONFIG) --silence-errors --libs libcurl)
LDLIBS += $(GLIB2_LIBS) $(LIBCRYPTO_LIBS) $(LIBCURL_LIBS)
LDLIBS += $(GLIB2_LIBS) $(LIBCRYPTO_LIBS)
WARNINGS := -Wall -Wextra -Wshadow \
-Wcast-align -Wwrite-strings -Wmissing-prototypes \
-Wmissing-declarations -Wredundant-decls -Wnested-externs \
@@ -24,18 +21,15 @@ WARNINGS := -Wall -Wextra -Wshadow \
ALL_CFLAGS += -DOPENSSL_API_COMPAT=0x10101000L \
$(GLIB2_CFLAGS) \
$(LIBCRYPTO_CFLAGS) \
$(LIBCURL_CFLAGS) \
$(WARNINGS) \
$(NULL)
BUILD_TARGETS := skip-$(LIB)
ifneq (${HAVE_OPENSSL},0)
ifneq (${HAVE_GLIB2},0)
ifneq (${HAVE_LIBCURL},0)
BUILD_TARGETS := $(LIB)
endif
endif
endif
sources := $(wildcard *.c)
objects := $(patsubst %.c,%.o,$(sources))
@@ -82,9 +76,4 @@ skip-$(LIB):
"openssl-devel / libssl-dev version >= 1.1.1", \
"HAVE_OPENSSL=0", \
"-I.")
$(call check_dep, \
"$(LIB)", \
"curl/curl.h", \
"libcurl-devel", \
"HAVE_LIBCURL=0")
touch $@

File diff suppressed because it is too large Load Diff

View File

@@ -1,45 +0,0 @@
/*
* Libpv common functions.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*
*/
/* Must be included before any other header */
#include "config.h"
#include "libpv/common.h"
#include "libpv/cert.h"
#include "libpv/curl.h"
/* setup and tear down */
int pv_init(void)
{
static size_t openssl_initalized;
if (g_once_init_enter(&openssl_initalized)) {
if (OPENSSL_VERSION_NUMBER < 0x1000100fL)
g_assert_not_reached();
#if OPENSSL_VERSION_NUMBER < 0x10100000L
SSL_library_init();
SSL_load_error_strings();
#else
OPENSSL_init_crypto(0, NULL);
#endif
if (pv_curl_init() != 0)
return -1;
pv_cert_init();
g_once_init_leave(&openssl_initalized, 1);
}
return 0;
}
void pv_cleanup(void)
{
pv_cert_cleanup();
pv_curl_cleanup();
}

View File

@@ -17,7 +17,6 @@
#include "lib/zt_common.h"
#include "libpv/crypto.h"
#include "libpv/glib-helper.h"
#include "libpv/hash.h"
char *pv_get_openssl_errors(void)
{
@@ -46,59 +45,6 @@ int pv_BIO_reset(BIO *b)
return 1;
}
GBytes *pv_generate_rand_data(size_t size, GError **error)
{
g_autofree uint8_t *data = NULL;
if (size > INT_MAX) {
g_set_error_literal(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_RANDOMIZATION,
"Too many random data requested. Split it up");
OPENSSL_clear_free(data, size);
return NULL;
}
data = g_malloc(size);
if (RAND_bytes(data, (int)size) != 1) {
g_set_error_literal(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_RANDOMIZATION,
"The required amount of random data is not available");
return NULL;
}
return pv_sec_gbytes_new_take(g_steal_pointer(&data), size);
}
GBytes *pv_generate_key(const EVP_CIPHER *cipher, GError **error)
{
int size;
pv_wrapped_g_assert(cipher);
size = EVP_CIPHER_key_length(cipher);
if (size <= 0) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_KEYGENERATION,
"Unknown cipher");
return NULL;
}
return pv_generate_rand_data((guint)size, error);
}
GBytes *pv_generate_iv(const EVP_CIPHER *cipher, GError **error)
{
int size;
pv_wrapped_g_assert(cipher);
size = EVP_CIPHER_iv_length(cipher);
if (size <= 0) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_KEYGENERATION,
"Unknown cipher");
return NULL;
}
return pv_generate_rand_data((guint)size, error);
}
static int64_t pv_gcm_encrypt_decrypt(GBytes *input, GBytes *aad, const PvCipherParms *parms,
GBytes **output, GBytes **tagp, enum PvCryptoMode mode,
GError **error)
@@ -360,168 +306,6 @@ GBytes *pv_hkdf_extract_and_expand(size_t derived_key_len, GBytes *key, GBytes *
return pv_sec_gbytes_new_take(g_steal_pointer(&derived_key), derived_key_len);
}
EVP_PKEY *pv_generate_ec_key(int nid, GError **error)
{
g_autoptr(EVP_PKEY_CTX) ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
g_autoptr(EVP_PKEY) ret = NULL;
g_assert(ctx);
if (EVP_PKEY_keygen_init(ctx) != 1) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_KEYGENERATION,
_("EC key could not be auto-generated"));
return NULL;
}
if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) != 1) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_KEYGENERATION,
_("EC key could not be auto-generated"));
return NULL;
}
if (EVP_PKEY_keygen(ctx, &ret) != 1) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_KEYGENERATION,
_("EC key could not be auto-generated"));
return NULL;
}
return g_steal_pointer(&ret);
}
/* Convert a EVP_PKEY to the key format used in the PV header */
PvEcdhPubKey *pv_evp_pkey_to_ecdh_pub_key(EVP_PKEY *key, GError **error)
{
g_autofree PvEcdhPubKey *ret = g_new0(PvEcdhPubKey, 1);
g_autoptr(BIGNUM) pub_x_big = NULL, pub_y_big = NULL;
g_autoptr(EC_KEY) ec_key = NULL;
const EC_POINT *pub_key;
const EC_GROUP *grp;
pv_wrapped_g_assert(key);
ec_key = EVP_PKEY_get1_EC_KEY(key);
if (!ec_key) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_INTERNAL,
_("Key has the wrong type"));
return NULL;
}
pub_key = EC_KEY_get0_public_key(ec_key);
if (!pub_key) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_INTERNAL,
_("Failed to get public key"));
return NULL;
}
grp = EC_KEY_get0_group(ec_key);
if (!grp) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_INTERNAL,
_("Failed to get EC group"));
return NULL;
}
pub_x_big = BN_new();
if (!pub_x_big)
g_abort();
pub_y_big = BN_new();
if (!pub_y_big)
g_abort();
if (EC_POINT_get_affine_coordinates_GFp(grp, pub_key, pub_x_big, pub_y_big, NULL) != 1) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_INTERNAL,
_("Cannot convert key to internal format"));
return NULL;
}
if (BN_bn2binpad(pub_x_big, ret->x, sizeof(ret->x)) < 0) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_INTERNAL,
_("Cannot convert key to internal format"));
return NULL;
}
if (BN_bn2binpad(pub_y_big, ret->y, sizeof(ret->y)) < 0) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_INTERNAL,
_("Cannot convert key to internal format"));
return NULL;
}
return g_steal_pointer(&ret);
}
static GBytes *derive_key(EVP_PKEY *key1, EVP_PKEY *key2, GError **error)
{
g_autoptr(EVP_PKEY_CTX) ctx = NULL;
uint8_t *data = NULL;
size_t data_size, key_size;
ctx = EVP_PKEY_CTX_new(key1, NULL);
if (!ctx)
g_abort();
if (EVP_PKEY_derive_init(ctx) != 1) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_INTERNAL,
_("Key derivation failed"));
return NULL;
}
if (EVP_PKEY_derive_set_peer(ctx, key2) != 1) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_INTERNAL,
_("Key derivation failed"));
return NULL;
}
/* Determine buffer length */
if (EVP_PKEY_derive(ctx, NULL, &key_size) != 1) {
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_DERIVE,
_("Key derivation failed"));
return NULL;
}
data_size = key_size;
data = OPENSSL_malloc(data_size);
if (!data)
g_abort();
if (EVP_PKEY_derive(ctx, data, &data_size) != 1) {
OPENSSL_clear_free(data, data_size);
g_set_error(error, PV_CRYPTO_ERROR, PV_CRYPTO_ERROR_DERIVE,
_("Key derivation failed"));
return NULL;
}
g_assert(data_size == key_size);
return pv_sec_gbytes_new_take(g_steal_pointer(&data), data_size);
}
GBytes *pv_derive_exchange_key(EVP_PKEY *cust, EVP_PKEY *host, GError **error)
{
const guint8 append[] = { 0x00, 0x00, 0x00, 0x01 };
g_autoptr(GBytes) derived_key = NULL, ret = NULL;
g_autoptr(GByteArray) der_key_ga = NULL;
g_autofree uint8_t *raw = NULL;
size_t raw_len;
pv_wrapped_g_assert(cust);
pv_wrapped_g_assert(host);
derived_key = derive_key(cust, host, error);
if (!derived_key)
return NULL;
der_key_ga = g_bytes_unref_to_array(g_steal_pointer(&derived_key));
/* ANSI X.9.63-2011: 66 bytes x with leading 7 bits and
* concatenate 32 bit int '1'
*/
der_key_ga = g_byte_array_append(der_key_ga, append, sizeof(append));
/* free GBytesArray and get underlying data */
raw_len = der_key_ga->len;
raw = g_byte_array_free(g_steal_pointer(&der_key_ga), FALSE);
ret = pv_sha256_hash(raw, raw_len, error);
OPENSSL_cleanse(raw, raw_len);
return g_steal_pointer(&ret);
}
GQuark pv_crypto_error_quark(void)
{
return g_quark_from_static_string("pv-crypto-error-quark");

View File

@@ -1,116 +0,0 @@
/*
* Libcurl utils
*
* Copyright IBM Corp. 2020
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include <curl/curl.h>
#include <stdio.h>
#include "lib/zt_common.h"
#include "libpv/curl.h"
struct UserData {
GByteArray *buffer;
uint max_size;
};
static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
g_assert(userdata);
struct UserData *data = (struct UserData *)userdata;
GByteArray *buffer = data->buffer;
uint64_t actual_size;
size_t err;
g_assert(buffer);
if (!g_uint64_checked_mul(&actual_size, size, nmemb))
g_abort();
/* Signal an error condition by returning a amount that differs
* from the amount passed to the callback. This results in a
* CURLE_WRITE_ERROR.
*/
err = actual_size + 1;
if (actual_size > G_MAXUINT)
return err;
data->buffer = g_byte_array_append(buffer, (uint8_t *)ptr, (uint)actual_size);
if (data->buffer->len > data->max_size)
return err;
return actual_size;
}
int pv_curl_init(void)
{
if (curl_global_init(CURL_GLOBAL_ALL) != 0)
return -1;
return 0;
}
void pv_curl_cleanup(void)
{
curl_global_cleanup();
}
GByteArray *curl_download(const char *url, long timeout_ms, uint max_size, GError **err)
{
g_autoptr(GByteArray) ret = NULL;
g_autoptr(CURL) handle = NULL;
g_autofree char *agent = NULL;
struct UserData userdata;
CURLcode rc;
/* set up curl session */
handle = curl_easy_init();
if (!handle)
g_abort();
/* follow redirection */
rc = curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
if (rc != CURLE_OK)
goto curl_err;
rc = curl_easy_setopt(handle, CURLOPT_TIMEOUT_MS, timeout_ms);
if (rc != CURLE_OK)
goto curl_err;
rc = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
if (rc != CURLE_OK)
goto curl_err;
agent = g_strdup_printf("%s/%s", GETTEXT_PACKAGE, RELEASE_STRING);
rc = curl_easy_setopt(handle, CURLOPT_USERAGENT, agent);
if (rc != CURLE_OK)
goto curl_err;
rc = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
if (rc != CURLE_OK)
goto curl_err;
ret = g_byte_array_new();
userdata.buffer = ret;
userdata.max_size = max_size;
rc = curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&userdata);
if (rc != CURLE_OK)
goto curl_err;
rc = curl_easy_setopt(handle, CURLOPT_URL, url);
if (rc != CURLE_OK)
goto curl_err;
rc = curl_easy_perform(handle);
if (rc != CURLE_OK) {
g_set_error(err, PV_CURL_ERROR, PV_CURL_ERROR_DOWNLOAD_FAILED,
_("download failed: %s"), curl_easy_strerror(rc));
return NULL;
}
return g_steal_pointer(&ret);
curl_err:
g_set_error(err, PV_CURL_ERROR, PV_CURL_ERROR_CURL_INIT_FAILED,
_("cURL initialization failed: %s"), curl_easy_strerror(rc));
return NULL;
}

View File

@@ -1,153 +0,0 @@
/*
* Hashing functions.
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include "libpv/crypto.h"
#include "libpv/hash.h"
GBytes *pv_sha256_hash(uint8_t *buf, size_t size, GError **error)
{
g_autoptr(EVP_MD_CTX) ctx = NULL;
ctx = pv_digest_ctx_new(EVP_sha256(), error);
if (!ctx)
return NULL;
if (pv_digest_ctx_update_raw(ctx, buf, size, error) != 0)
return NULL;
return pv_digest_ctx_finalize(ctx, error);
}
EVP_MD_CTX *pv_digest_ctx_new(const EVP_MD *md, GError **error)
{
g_autoptr(EVP_MD_CTX) ctx = EVP_MD_CTX_new();
if (!ctx) {
g_set_error(error, PV_HASH_ERROR, PV_HASH_ERROR_INTERNAL,
_("Hash context generation failed"));
return NULL;
}
if (EVP_DigestInit_ex(ctx, md, NULL) != 1) {
g_set_error(error, PV_HASH_ERROR, PV_HASH_ERROR_INTERNAL,
_("EVP_DigestInit_ex failed"));
return NULL;
}
return g_steal_pointer(&ctx);
}
int pv_digest_ctx_update_raw(EVP_MD_CTX *ctx, const uint8_t *buf, size_t size, GError **error)
{
if (!buf || size == 0)
return 0;
if (EVP_DigestUpdate(ctx, buf, size) != 1) {
g_set_error(error, PV_HASH_ERROR, PV_HASH_ERROR_INTERNAL,
_("EVP_DigestUpdate failed"));
return -1;
}
return 0;
}
int pv_digest_ctx_update(EVP_MD_CTX *ctx, GBytes *data, GError **error)
{
const uint8_t *buf;
size_t buf_size;
if (!data)
return 0;
buf = g_bytes_get_data((GBytes *)data, &buf_size);
return pv_digest_ctx_update_raw(ctx, buf, buf_size, error);
}
GBytes *pv_digest_ctx_finalize(EVP_MD_CTX *ctx, GError **error)
{
int md_size = EVP_MD_size(EVP_MD_CTX_md(ctx));
g_autofree uint8_t *digest = NULL;
unsigned int digest_size;
g_assert(md_size > 0);
digest = g_malloc0((uint)md_size);
if (EVP_DigestFinal_ex(ctx, digest, &digest_size) != 1) {
g_set_error(error, PV_HASH_ERROR, PV_HASH_ERROR_INTERNAL,
_("EVP_DigestFinal_ex failed"));
return NULL;
}
g_assert(digest_size == (uint)md_size);
return g_bytes_new_take(g_steal_pointer(&digest), digest_size);
}
HMAC_CTX *pv_hmac_ctx_new(GBytes *key, const EVP_MD *md, GError **error)
{
g_autoptr(HMAC_CTX) ctx = HMAC_CTX_new();
const uint8_t *key_data;
size_t key_size;
key_data = g_bytes_get_data(key, &key_size);
if (HMAC_Init_ex(ctx, key_data, (int)key_size, md, NULL) != 1) {
g_autofree char *openssl_err_msg = pv_get_openssl_errors();
g_set_error(error, PV_HASH_ERROR, PV_HASH_ERROR_INTERNAL,
"unable to create HMAC context: %s", openssl_err_msg);
return NULL;
}
return g_steal_pointer(&ctx);
}
int pv_hmac_ctx_update_raw(HMAC_CTX *ctx, const void *buf, size_t size, GError **error)
{
if (!buf || size == 0)
return 0;
if (HMAC_Update(ctx, buf, size) != 1) {
g_autofree char *openssl_err_msg = pv_get_openssl_errors();
g_set_error(error, PV_HASH_ERROR, PV_HASH_ERROR_INTERNAL,
"unable to add data to HMAC context: %s", openssl_err_msg);
return -1;
}
return 0;
}
int pv_hmac_ctx_update(HMAC_CTX *ctx, GBytes *data, GError **error)
{
const uint8_t *buf;
size_t buf_size;
if (!data)
return 0;
buf = g_bytes_get_data((GBytes *)data, &buf_size);
return pv_hmac_ctx_update_raw(ctx, buf, buf_size, error);
}
GBytes *pv_hamc_ctx_finalize(HMAC_CTX *ctx, GError **error)
{
int md_size = EVP_MD_size(HMAC_CTX_get_md(ctx));
g_autofree uint8_t *hmac = NULL;
unsigned int hmac_size = 0;
g_assert(md_size > 0);
hmac = g_malloc0((unsigned int)md_size);
if (HMAC_Final(ctx, hmac, &hmac_size) != 1) {
g_autofree char *openssl_err_msg = pv_get_openssl_errors();
g_set_error(error, PV_HASH_ERROR, PV_HASH_ERROR_INTERNAL,
"unable to calculate HMAC: %s", openssl_err_msg);
return NULL;
}
return g_bytes_new_take(g_steal_pointer(&hmac), hmac_size);
}

View File

@@ -90,10 +90,10 @@ const char *util_arch_machine_type_to_str(int type)
case UTIL_ARCH_MACHINE_TYPE_Z14_ZR1:
return "IBM z14 ZR1";
case UTIL_ARCH_MACHINE_TYPE_Z15:
return "IBM z15";
case UTIL_ARCH_MACHINE_TYPE_Z15_T02:
return "IBM z15 Model T02";
return "IBM z15";
case UTIL_ARCH_MACHINE_TYPE_Z16:
case UTIL_ARCH_MACHINE_TYPE_Z16_A02:
return "IBM z16";
default:
return "Unknown machine type";
@@ -111,6 +111,7 @@ unsigned long util_arch_hsa_maxsize(void)
case UTIL_ARCH_MACHINE_TYPE_Z15:
case UTIL_ARCH_MACHINE_TYPE_Z15_T02:
case UTIL_ARCH_MACHINE_TYPE_Z16:
case UTIL_ARCH_MACHINE_TYPE_Z16_A02:
return HSA_SIZE_512M;
default:
return HSA_SIZE_32M;

View File

@@ -27,7 +27,7 @@ void util_hexdump_grp(FILE *fh, const char *tag, const void *data, int grp,
for (i = 0; i < count; i++) {
if (first) {
fprintf(fh, "%*s", indent, " ");
fprintf(fh, "%*s", indent, "");
if (tag)
fprintf(fh, "%s: ", tag);
fprintf(fh, "%08x: ", i);

View File

@@ -110,7 +110,8 @@ static int file_puts(const char *str, const char *path)
}
rc = 0;
out_fclose:
fclose(fp);
if (fclose(fp))
return -1;
return rc;
}

763
libutil/util_fmt.c Normal file
View File

@@ -0,0 +1,763 @@
/*
* util - Utility function library
*
* Format structured data as key-value pairs, JSON, or CSV
*
* Copyright IBM Corp. 2024
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "lib/util_base.h"
#include "lib/util_fmt.h"
#include "lib/util_libc.h"
#include "lib/util_rec.h"
#include "lib/zt_common.h"
struct obj_t {
char *name;
bool is_list;
bool is_row;
bool is_prefix;
unsigned int index;
};
struct key_t {
char *name;
bool persist;
};
static struct {
enum util_fmt_t type;
FILE *fd;
int fileno;
/* Format control. */
bool hide_prefix;
bool hide_inval;
bool quote_all;
bool do_filter;
bool do_warn;
bool hide_meta;
bool handle_int;
int api_level;
const char *nl;
/* JSON specifics. */
unsigned int ind_base;
unsigned int ind_width;
char ind_char;
bool meta_done;
/* CSV specifics. */
struct util_rec *csv_rec;
bool csv_hdr;
bool csv_data;
/* State. */
unsigned int lvl;
struct obj_t *objs;
unsigned int num_objs;
struct key_t *keys;
unsigned int num_keys;
struct sigaction old_int;
struct sigaction old_term;
/* Methods. */
void (*obj_start)(struct obj_t *parent, struct obj_t *obj);
void (*obj_end)(struct obj_t *parent, struct obj_t *obj);
void (*map)(struct obj_t *parent, unsigned int mflags, const char *key,
const char *val);
void (*term)(void);
} f;
#define fwarn(fmt, ...) \
do { if (f.do_warn) warnx(fmt, ##__VA_ARGS__); } while (0)
/* Map format name to format ID. */
static const struct {
const char *name;
enum util_fmt_t fmt;
} formats[] = {
{ "json", FMT_JSON },
{ "json-seq", FMT_JSONSEQ },
{ "pairs", FMT_PAIRS },
{ "csv", FMT_CSV },
};
/* Signal mask for blocking INT and TERM signals. */
static sigset_t no_int_mask;
bool util_fmt_name_to_type(const char *name, enum util_fmt_t *type)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(formats); i++) {
if (strcasecmp(name, formats[i].name) == 0) {
*type = formats[i].fmt;
return true;
}
}
return false;
}
static void safe_write(const char *str)
{
size_t done, todo;
ssize_t rc;
if (f.fileno < 0)
return;
for (done = 0; (todo = strlen(&str[done])) > 0; done += (size_t)rc) {
rc = write(f.fileno, &str[done], todo);
if (rc <= 0)
return;
}
}
static void _indent(unsigned int off, bool safe)
{
unsigned int num, i;
if (f.type == FMT_JSONSEQ)
return;
num = f.ind_base + off;
if (f.type == FMT_JSON && f.lvl > 0)
num += f.lvl - 1;
for (i = 0; i < num * f.ind_width; i++) {
if (!safe) {
fputc(f.ind_char, f.fd);
} else if (f.fileno >= 0) {
if (write(f.fileno, &f.ind_char, 1) <= 0)
return;
}
}
}
#define indent(x) _indent(x, false)
static void obj_free(struct obj_t *obj)
{
free(obj->name);
memset(obj, 0, sizeof(*obj));
}
static void disable_int(sigset_t *saved)
{
if (f.handle_int)
sigprocmask(SIG_BLOCK, &no_int_mask, saved);
}
static void enable_int(sigset_t *saved)
{
if (f.handle_int) {
/* Ensure latest updates are flushed to file descriptor. */
fflush(f.fd);
sigprocmask(SIG_SETMASK, saved, NULL);
}
}
static void int_handler(int signum)
{
struct sigaction *old;
if (f.term)
f.term();
/* Re-install and call original handler. */
old = (signum == SIGINT) ? &f.old_int : &f.old_term;
sigaction(signum, old, NULL);
raise(signum);
}
static void setup_int_handler(void)
{
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = &int_handler;
sigaction(SIGINT, &act, &f.old_int);
sigaction(SIGTERM, &act, &f.old_term);
}
static void remove_int_handler(void)
{
sigaction(SIGINT, &f.old_int, NULL);
sigaction(SIGTERM, &f.old_term, NULL);
}
void util_fmt_exit(void)
{
unsigned int i;
if (f.handle_int)
remove_int_handler();
if (f.lvl > 0)
fwarn("%s before remaining %d util_obj_end()", __func__, f.lvl);
for (i = 0; i < f.num_keys; i++)
free(f.keys[i].name);
free(f.keys);
for (i = 0; i < f.num_objs; i++)
obj_free(&f.objs[i]);
free(f.objs);
if (f.type == FMT_CSV)
util_rec_free(f.csv_rec);
}
void util_fmt_set_indent(unsigned int base, unsigned int width, char ind_char)
{
f.ind_base = base;
f.ind_width = width;
f.ind_char = ind_char;
}
static unsigned int to_hex(char *str, int val, unsigned int num_digits)
{
int digit;
char *c;
for (c = str + num_digits - 1; c >= str; c--) {
digit = (val & 0xf);
val >>= 4;
*c = (char)((digit >= 10) ? digit - 10 + 'a' : digit + '0');
}
return num_digits;
}
static char get_escape(const char *map, char c)
{
int i;
for (i = 0; map[i] && map[i + 1]; i += 2) {
if (map[i] == c)
return map[i + 1];
}
return 0;
}
struct quote_params {
const char *double_chars;
const char *esc_map;
char hex_char;
unsigned int hex_digits;
unsigned int max_width_per_char;
};
static char *do_quote(const char *str, const struct quote_params *p)
{
unsigned int from, to;
char *q, esc, c;
/* Start with worst-case length assuming every char is replaced. */
q = util_zalloc(strlen(str) * p->max_width_per_char + /* "" nul */ 3);
to = 0;
q[to++] = '"';
for (from = 0; (c = str[from]); from++) {
if (p->double_chars && strchr(p->double_chars, c)) {
/* Escape characters by doubling them ("" in CSV). */
q[to++] = c;
q[to++] = c;
} else if (p->esc_map && (esc = get_escape(p->esc_map, c))) {
/* Escape characters with backslash + letter. */
q[to++] = '\\';
q[to++] = esc;
} else if (p->hex_char && !isprint(c)) {
/* Escape characters with backslash + hex code. */
q[to++] = '\\';
q[to++] = p->hex_char;
to += to_hex(&q[to], c, p->hex_digits);
} else {
q[to++] = c;
}
}
q[to++] = '"';
return util_realloc(q, (size_t)to + 1);
}
static char *csv_quote(const char *str)
{
static const struct quote_params csv_quote_params = {
.double_chars = "\"",
.esc_map = NULL,
.hex_char = 0,
.hex_digits = 0,
.max_width_per_char = 2 /* " => "" */,
};
return do_quote(str, &csv_quote_params);
}
static void add_key(const char *name, bool persist)
{
struct key_t key;
char *hdr;
key.name = util_strdup(name);
key.persist = persist;
util_add_array(&f.keys, &f.num_keys, key);
if (f.type == FMT_CSV) {
hdr = csv_quote(name);
util_rec_def(f.csv_rec, name, UTIL_REC_ALIGN_LEFT, 0, hdr);
free(hdr);
util_rec_set(f.csv_rec, name, "\"\"");
f.csv_hdr = true;
}
}
static struct key_t *get_key(const char *name)
{
unsigned int i;
for (i = 0; i < f.num_keys; i++) {
if (strcmp(name, f.keys[i].name) == 0)
return &f.keys[i];
}
return NULL;
}
void util_fmt_add_key(const char *fmt, ...)
{
va_list args;
char *key;
va_start(args, fmt);
util_vasprintf(&key, fmt, args);
va_end(args);
/* Only add unique keys. */
if (!get_key(key))
add_key(key, true);
free(key);
}
static bool update_key(const char *name, bool persist)
{
struct key_t *key;
bool rc = true;
key = get_key(name);
if (key) {
key->persist = persist;
} else if (!f.do_filter) {
add_key(name, persist);
} else {
fwarn("util_fmt_pair for key '%s' without util_fmt_add_key()",
name);
rc = false;
}
return rc;
}
static struct obj_t *curr_obj(int off)
{
int lvl = (int)f.lvl - 1 + off;
return lvl < 0 ? NULL : &f.objs[lvl];
}
static void _util_fmt_obj_end(void);
/*
* By s390-tools convention, all tool output must be contained in an extra
* top-level object that includes tool-invocation meta-data.
*/
static void emit_meta_object(void)
{
unsigned int quoted = FMT_PERSIST | FMT_QUOTE, unquoted = FMT_PERSIST;
char hostname[HOST_NAME_MAX + 1] = { 0 }, date[30];
struct timeval tv;
struct tm *tm;
f.meta_done = true;
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_obj_start(FMT_PREFIX, "meta");
/*
* "meta": {
* "api_level": 1,
* "version": "2.32.0",
* "host": "localhost",
* "time_epoch": 1714392976,
* "time": "2024-04-29 14:16:16+0200",
* }
*/
util_fmt_pair(unquoted, "api_level", "%d", f.api_level);
util_fmt_pair(quoted, "version", "%s", RELEASE_STRING);
gethostname(hostname, sizeof(hostname) - 1);
util_fmt_pair(quoted, "host", "%s", hostname);
gettimeofday(&tv, NULL);
util_fmt_pair(unquoted, "time_epoch", "%llu", tv.tv_sec);
tm = localtime(&tv.tv_sec);
if (!strftime(date, sizeof(date), "%F %T%z", tm))
date[0] = 0;
util_fmt_pair(quoted, "time", "%s", date);
_util_fmt_obj_end();
if (f.type == FMT_JSONSEQ) {
/* Tool meta-data is a separate object for JSONSEQ. */
util_fmt_obj_end();
}
}
void util_fmt_obj_start(unsigned int oflags, const char *fmt, ...)
{
struct obj_t *parent, *obj;
char *name = NULL;
sigset_t set;
va_list args;
if (!f.hide_meta && !f.meta_done && f.lvl == 0) {
emit_meta_object();
/*
* Allow override of top-level key name for supplementary
* output formats.
*/
if (!fmt)
name = util_strdup(program_invocation_short_name);
}
if (fmt) {
va_start(args, fmt);
util_vasprintf(&name, fmt, args);
va_end(args);
}
f.lvl++;
if (f.lvl > f.num_objs)
util_expand_array(&f.objs, &f.num_objs);
parent = curr_obj(-1);
obj = curr_obj(0);
obj->name = name;
obj->is_list = (oflags & FMT_LIST);
obj->is_row = (oflags & FMT_ROW);
obj->is_prefix = (oflags & FMT_PREFIX);
obj->index = 0;
if (f.obj_start) {
disable_int(&set);
f.obj_start(parent, obj);
enable_int(&set);
}
if (parent)
parent->index++;
}
static void _util_fmt_obj_end(void)
{
struct obj_t *obj, *parent;
sigset_t set;
if (f.lvl == 0) {
fwarn("%s without util_fmt_obj_start", __func__);
return;
}
parent = curr_obj(-1);
obj = curr_obj(0);
if (f.obj_end) {
disable_int(&set);
f.obj_end(parent, obj);
enable_int(&set);
}
f.lvl--;
obj_free(obj);
}
void util_fmt_obj_end(void)
{
_util_fmt_obj_end();
if (f.lvl == 1 && f.meta_done && f.type != FMT_JSONSEQ) {
/* Emit closure for top-level meta-container object. */
util_fmt_obj_end();
}
}
static char *add_prefix(const char *str, bool full)
{
struct obj_t *obj;
unsigned int i;
char *prefix;
prefix = util_strdup("");
for (i = 0; i < f.lvl; i++) {
obj = &f.objs[i];
if (!full && !obj->is_prefix)
continue;
if (obj->name) {
if (*prefix)
util_concatf(&prefix, ".");
util_concatf(&prefix, "%s", obj->name);
}
if (obj->is_list && full)
util_concatf(&prefix, "[%d]", obj->index - 1);
}
if (*prefix)
util_concatf(&prefix, ".");
util_concatf(&prefix, "%s", str);
return prefix;
}
void util_fmt_pair(unsigned int mflags, const char *key, const char *fmt, ...)
{
char *val, *prefixed_key;
struct obj_t *obj;
bool is_filtered;
sigset_t set;
va_list args;
obj = curr_obj(0);
if (!obj) {
fwarn("%s before util_fmt_obj_start", __func__);
return;
}
/* Filter by key. */
if (f.do_filter) {
prefixed_key = add_prefix(key, false);
is_filtered = !get_key(prefixed_key);
free(prefixed_key);
if (is_filtered)
return;
}
/* Filter by validity. */
if (f.hide_inval && (mflags & FMT_INVAL))
return;
va_start(args, fmt);
util_vasprintf(&val, fmt, args);
va_end(args);
if (f.map) {
disable_int(&set);
f.map(obj, mflags, key, val);
enable_int(&set);
}
obj->index++;
free(val);
}
static char *pairs_quote(const char *str)
{
static const struct quote_params pairs_quote_params = {
.double_chars = NULL,
.esc_map = "\"\"$$``\\\\\aa\bb\ee\ff\nn\rr\tt\vv",
.hex_char = 'x',
.hex_digits = 2,
.max_width_per_char = 4 /* '\x' + 2 hex_digits */,
};
return do_quote(str, &pairs_quote_params);
}
static void pairs_map(struct obj_t *UNUSED(obj), unsigned int mflags,
const char *key, const char *val)
{
char *full_key, *qval = NULL;
if (mflags & FMT_INVAL)
val = "";
indent(0);
if (f.quote_all || (mflags & FMT_QUOTE))
qval = pairs_quote(val);
if (f.hide_prefix) {
fprintf(f.fd, "%s=%s\n", key, qval ?: val);
} else {
full_key = add_prefix(key, true);
fprintf(f.fd, "%s=%s\n", full_key, qval ?: val);
free(full_key);
}
free(qval);
}
static char *json_quote(const char *str)
{
static const struct quote_params json_quote_params = {
.double_chars = NULL,
.esc_map = "\"\"\\\\\bb\ff\nn\rr\tt",
.hex_char = 'u',
.hex_digits = 4,
.max_width_per_char = 6 /* '\u' + 4 hex_digits */,
};
return do_quote(str, &json_quote_params);
}
static void json_obj_start(struct obj_t *parent, struct obj_t *obj)
{
char *key;
if (!parent && f.type == FMT_JSONSEQ) {
/* Emit leading record separator according to RFC 7464. */
fprintf(f.fd, "\x1e");
}
if (parent && parent->index > 0)
fprintf(f.fd, ",%s", f.nl);
indent(0);
if (parent && !parent->is_list && obj->name) {
key = json_quote(obj->name);
fprintf(f.fd, "%s: ", key);
free(key);
}
fprintf(f.fd, obj->is_list ? "[%s" : "{%s", f.nl);
}
static void json_obj_end(struct obj_t *parent, struct obj_t *obj)
{
if (obj->index > 0)
fprintf(f.fd, "%s", f.nl);
indent(0);
fprintf(f.fd, obj->is_list ? "]" : "}");
if (!parent)
fprintf(f.fd, "\n");
}
/*
* Ensure syntactically correct JSON by emitting all pending closure elements.
* Called in signal context - only use signal-safe functions.
*/
static void json_term(void)
{
struct obj_t *obj, *parent;
for (; f.lvl > 0; f.lvl--) {
obj = curr_obj(0);
parent = curr_obj(-1);
if (obj->index > 0)
safe_write(f.nl);
_indent(0, true);
safe_write(obj->is_list ? "]" : "}");
if (!parent)
safe_write(f.nl);
}
}
static void json_map(struct obj_t *parent, unsigned int mflags,
const char *key, const char *val)
{
char *qkey, *qval = NULL;
qkey = json_quote(key);
if (mflags & FMT_INVAL)
qval = util_strdup("null");
else if (f.quote_all || (mflags & FMT_QUOTE))
qval = json_quote(val);
if (parent->index > 0)
fprintf(f.fd, ",%s", f.nl);
indent(1);
fprintf(f.fd, "%s: %s", qkey, qval ?: val);
free(qval);
free(qkey);
}
static void csv_obj_start(struct obj_t *UNUSED(parent), struct obj_t *obj)
{
if (!obj->is_row)
return;
}
static void csv_obj_end(struct obj_t *UNUSED(parent), struct obj_t *obj)
{
unsigned int i;
if (!(obj->is_row || (f.lvl == 1 && f.csv_data)))
return;
if (f.csv_hdr) {
/* Print row with CSV header. */
indent(0);
util_rec_print_hdr(f.csv_rec);
f.csv_hdr = false;
}
/* Print row with CSV data. */
indent(0);
util_rec_print(f.csv_rec);
f.csv_data = false;
/* Reset non-persistent fields. */
for (i = 0; i < f.num_keys; i++) {
if (!f.keys[i].persist)
util_rec_set(f.csv_rec, f.keys[i].name, "\"\"");
}
}
static void csv_map(struct obj_t *UNUSED(obj), unsigned int mflags,
const char *key, const char *val)
{
char *qval = NULL, *prefixed_key;
/* Use empty string for invalid values. */
if (mflags & FMT_INVAL)
val = "";
/* Quote value if requested. */
if (f.quote_all || (mflags & FMT_QUOTE))
qval = csv_quote(val);
/* Process key and value. */
prefixed_key = add_prefix(key, false);
if (update_key(prefixed_key, mflags & FMT_PERSIST)) {
util_rec_set(f.csv_rec, prefixed_key, "%s", qval ?: val);
f.csv_data = true;
}
free(prefixed_key);
free(qval);
}
void util_fmt_init(FILE *fd, enum util_fmt_t type, unsigned int flags,
int api_level)
{
memset(&f, 0, sizeof(f));
f.type = type;
f.fd = fd;
f.fileno = fileno(fd);
f.hide_prefix = (flags & FMT_NOPREFIX);
f.hide_inval = !(flags & FMT_KEEPINVAL);
f.hide_meta = (flags & FMT_NOMETA);
f.quote_all = (flags & FMT_QUOTEALL);
f.do_filter = (flags & FMT_FILTER);
f.do_warn = (flags & FMT_WARN);
f.handle_int = (flags & FMT_HANDLEINT);
f.api_level = api_level;
if (type == FMT_JSONSEQ)
f.nl = "";
else
f.nl = "\n";
f.ind_width = 2;
f.ind_char = ' ';
f.meta_done = false;
switch (type) {
case FMT_PAIRS:
f.map = &pairs_map;
break;
case FMT_JSON:
case FMT_JSONSEQ:
f.obj_start = &json_obj_start;
f.obj_end = &json_obj_end;
f.map = &json_map;
f.term = &json_term;
break;
case FMT_CSV:
f.obj_start = &csv_obj_start;
f.obj_end = &csv_obj_end;
f.map = &csv_map;
f.csv_rec = util_rec_new_csv(",");
f.csv_hdr = true;
f.csv_data = false;
break;
}
/* Ensure consistent number format for callers that use setlocale(). */
setlocale(LC_NUMERIC, "C");
if (f.handle_int) {
setup_int_handler();
sigemptyset(&no_int_mask);
sigaddset(&no_int_mask, SIGINT);
sigaddset(&no_int_mask, SIGTERM);
}
}

251
libutil/util_fmt_example.c Normal file
View File

@@ -0,0 +1,251 @@
/*
* util_fmt_example - Example program for util_fmt
*
* Copyright IBM Corp. 2024
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdlib.h>
#include <string.h>
#include "lib/util_base.h"
#include "lib/util_fmt.h"
#define API_LEVEL 1
static void meta_example(enum util_fmt_t format)
{
util_fmt_init(stdout, format, FMT_DEFAULT, API_LEVEL);
/*
* First call to util_fmt_obj_start() automatically adds meta-data
* object as required by s390-tools convention.
*/
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_pair(FMT_QUOTE, "key", "value");
util_fmt_obj_end();
util_fmt_exit();
}
static void simple_example(enum util_fmt_t format, int fmt_flags)
{
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, fmt_flags | FMT_NOMETA, API_LEVEL);
/*
* {
* "child": {
* "key": "value",
* "invalid":"invalidvalue" <== Marked as invalid
* }
* }
*/
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_obj_start(FMT_DEFAULT, "child");
util_fmt_pair(FMT_QUOTE, "key", "value");
util_fmt_pair(FMT_QUOTE | FMT_INVAL, "invalid", "invalidvalue");
util_fmt_obj_end();
util_fmt_obj_end();
util_fmt_exit();
}
static void list_example(enum util_fmt_t format, int flags)
{
int i;
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, flags | FMT_NOMETA, API_LEVEL);
/*
* "cond","key"
* condvalue0,value0
* "",value1
* "",value2
* "",value3
*/
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_obj_start(FMT_LIST, "list");
for (i = 0; i < 4; i++) {
util_fmt_obj_start(FMT_ROW, NULL);
if (i == 0)
util_fmt_pair(flags, "cond", "condvalue%d", i);
util_fmt_pair(FMT_DEFAULT, "key", "value%d", i);
util_fmt_obj_end();
}
util_fmt_obj_end();
util_fmt_obj_end();
util_fmt_exit();
}
#define NUM_KEYS 4
static void vary_example(enum util_fmt_t format, bool add)
{
const char *keys[NUM_KEYS] = { "key_a", "key_b", "key_c", "key_d" };
int i;
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, FMT_NOMETA, API_LEVEL);
if (add) {
/* Make keys known before starting output. */
for (i = 0; i < NUM_KEYS; i++)
util_fmt_add_key(keys[i]);
}
util_fmt_obj_start(FMT_LIST, "list");
for (i = 0; i < 4; i++) {
util_fmt_obj_start(FMT_ROW, NULL);
util_fmt_pair(FMT_DEFAULT, keys[i], "value%d", i);
util_fmt_obj_end();
}
util_fmt_obj_end();
util_fmt_exit();
}
static void filter_example(enum util_fmt_t format)
{
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, FMT_FILTER | FMT_NOMETA, API_LEVEL);
util_fmt_add_key("key_a");
/*
* {
* "key_a": "value_a",
* "key_b": "value_b" <== Not announced via util_fmt_add_key()
* }
*/
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_pair(FMT_QUOTE, "key_a", "value_a");
util_fmt_pair(FMT_QUOTE, "key_b", "value_b");
util_fmt_obj_end();
util_fmt_exit();
}
static void prefix_example(enum util_fmt_t format, bool do_prefix)
{
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, FMT_NOMETA, API_LEVEL);
/*
* {
* "key": "value0",
* "obj1": { // Marked as prefix object
* "key": "value1"
* }
* }
*/
util_fmt_obj_start(FMT_DEFAULT, "obj0");
util_fmt_pair(FMT_QUOTE, "key", "value0");
util_fmt_obj_start(do_prefix ? FMT_PREFIX : FMT_DEFAULT, "obj1");
util_fmt_pair(FMT_QUOTE, "key", "value1");
util_fmt_obj_end();
util_fmt_obj_end();
util_fmt_exit();
}
static void announce(const char *example_name)
{
static int example_number;
int i;
if (example_number++ > 0)
printf("\n");
printf("%d. %s\n====", example_number, example_name);
for (i = strlen(example_name); i > 0; i--)
printf("=");
printf("\n");
}
int main(int UNUSED(argc), char *UNUSED(argv[]))
{
announce("JSON output");
simple_example(FMT_JSON, FMT_KEEPINVAL);
announce("JSON without invalid pairs");
simple_example(FMT_JSON, FMT_DEFAULT);
announce("JSON formatted as sequence");
simple_example(FMT_JSONSEQ, FMT_DEFAULT);
announce("Pairs output");
simple_example(FMT_PAIRS, FMT_KEEPINVAL);
announce("Pairs output without invalid pairs");
simple_example(FMT_PAIRS, FMT_DEFAULT);
announce("Pairs without prefix");
simple_example(FMT_PAIRS, FMT_NOPREFIX);
announce("CSV output");
simple_example(FMT_CSV, FMT_KEEPINVAL);
announce("CSV list output");
list_example(FMT_CSV, FMT_DEFAULT);
announce("CSV list with persistent cond value");
list_example(FMT_CSV, FMT_PERSIST);
announce("JSON with filtered key");
filter_example(FMT_JSON);
announce("Pairs with filtered key");
filter_example(FMT_PAIRS);
announce("CSV with filtered key");
filter_example(FMT_CSV);
announce("CSV list with varying keys");
vary_example(FMT_CSV, false);
announce("CSV list with pre-announced varying keys");
vary_example(FMT_CSV, true);
announce("JSON output with meta-data");
meta_example(FMT_JSON);
announce("JSON sequence output with meta-data");
meta_example(FMT_JSONSEQ);
announce("Pairs output with meta-data");
meta_example(FMT_PAIRS);
announce("CSV output with meta-data");
meta_example(FMT_CSV);
announce("JSON output with duplicate keys");
prefix_example(FMT_JSON, false);
announce("CSV output with duplicate keys");
prefix_example(FMT_CSV, false);
announce("CSV output with duplicate keys distinguished by prefix");
prefix_example(FMT_CSV, true);
return 0;
}

View File

@@ -139,6 +139,26 @@ char *util_strcat_realloc(char *str1, const char *str2)
return buf;
}
/**
* Concatenate a string with the result of a format string expansion
*
* @param[in, out] str1 Pointer to pointer to first string
* @param[in] fmt Format string for generation of the second string
* @param[in] ... Parameters for format string
*/
void util_concatf(char **str1, const char *fmt, ...)
{
va_list args;
char *str2;
va_start(args, fmt);
util_vasprintf(&str2, fmt, args);
va_end(args);
*str1 = util_strcat_realloc(*str1, str2);
free(str2);
}
/**
* Convert string to uppercase
*

View File

@@ -40,6 +40,14 @@ int main(void)
fprintf(stderr, "result = \"%s\"\n", str);
free(str);
/* Use util_concatf() for string concatenation */
fprintf(stderr, "Try to concatenate \"list\" plus comma-separated list of numbers 1 to 3: ");
str = NULL;
util_concatf(&str, "list:");
for (int i = 1; i <= 3; i++)
util_concatf(&str, "%s%d", (i > 1 ? "," : ""), i);
fprintf(stderr, "result = %s\n", str); /* list:part1,part2,part3 */
/* One byte allocation should work */
fprintf(stderr, "Try to allocate 1 byte: ");
ptr = util_malloc(1);

View File

@@ -25,8 +25,8 @@
#include "lib/util_panic.h"
#define WAITPID 120 /* Time to wait for pid to be written */
#define WAITINC 5 /* Additional time to wait each retry */
#define MAXWAIT 60 /* Maximum wait between retries */
#define DEF_WAITINC_US 5000000 /* Additional time to wait each retry */
#define DEF_MAXWAIT_US 60000000 /* Maximum wait between retries */
#define BUFSIZE 40 /* Buffer must be large enough to fit pid string */
/**
@@ -133,15 +133,20 @@ static int handle_stale_lock(char *lockfile)
*
* @param[in] lockfile Path to the lock file
* @param[in] retries Number of times to retry if lock fails initially
* @param[in] waitinc How many micro-seconds to extend wait time before
* additional retry
* @param[in] maxwait Maximum wait time before retry
* @param[in] pid PID to use for lock ownership
*
* @retval 0 Lock created with PID as owner
* @retval !=0 Lock was not created
*/
static int do_lockfile_lock(char *lockfile, unsigned int retries, int pid)
static int do_lockfile_lock(char *lockfile, unsigned int retries, int pid,
unsigned int waitinc, unsigned int maxwait)
{
int fd, plen, len, rc = 0, snooze = 0;
unsigned int tries = retries + 1;
int fd, plen, len, rc = 0;
unsigned int snooze = 0;
char buf[BUFSIZE];
char *tpath;
@@ -190,9 +195,9 @@ static int do_lockfile_lock(char *lockfile, unsigned int retries, int pid)
if (rc != 0) {
tries--;
if (tries > 0) {
snooze += WAITINC;
snooze = (snooze > MAXWAIT) ? MAXWAIT : snooze;
sleep(snooze);
snooze += waitinc;
snooze = (snooze > maxwait) ? maxwait : snooze;
usleep(snooze);
}
}
} while (tries > 0);
@@ -255,7 +260,27 @@ static int do_lockfile_release(char *lockfile, int pid)
*/
int util_lockfile_lock(char *lockfile, int retries)
{
return do_lockfile_lock(lockfile, retries, getpid());
return do_lockfile_lock(lockfile, retries, getpid(), DEF_WAITINC_US,
DEF_MAXWAIT_US);
}
/**
* Attempt to create a lockfile owned by this process at the specified path
* using a custom wait/retry time.
*
* @param[in] lockfile Path to the lock file
* @param[in] retries Number of times to retry if lock fails initially
* @param[in] waitinc How many micro-seconds to extend wait time before
* additional retry
* @param[in] maxwait Maximum wait time before retry
*
* @retval 0 Lock created
* @retval !=0 Lock was not created
*/
int util_lockfile_lock_cw(char *lockfile, int retries, unsigned int waitinc,
unsigned int maxwait)
{
return do_lockfile_lock(lockfile, retries, getpid(), waitinc, maxwait);
}
/**
@@ -270,7 +295,27 @@ int util_lockfile_lock(char *lockfile, int retries)
*/
int util_lockfile_parent_lock(char *lockfile, int retries)
{
return do_lockfile_lock(lockfile, retries, getppid());
return do_lockfile_lock(lockfile, retries, getppid(), DEF_WAITINC_US,
DEF_MAXWAIT_US);
}
/**
* Attempt to create a lockfile owned by the parent of this process at the
* specified path using a custom wait/retry time.
*
* @param[in] lockfile Path to the lock file
* @param[in] retries Number of times to retry if lock fails initially
* @param[in] waitinc How many micro-seconds to extend wait time before
* additional retry
* @param[in] maxwait Maximum wait time before retry
*
* @retval 0 Lock created
* @retval !=0 Lock was not created
*/
int util_lockfile_parent_lock_cw(char *lockfile, int retries,
unsigned int waitinc, unsigned int maxwait)
{
return do_lockfile_lock(lockfile, retries, getppid(), waitinc, maxwait);
}
/**

View File

@@ -111,7 +111,7 @@ const char *util_rec_fld_get_key(struct util_rec_fld *fld)
/**
* Create a new record with "wide" output format
*
* @param[in] hdr_sep Header separator
* @param[in] hdr_sep Header separator or %NULL for no separator
*
* @returns Pointer to the created record
*/
@@ -121,7 +121,7 @@ struct util_rec *util_rec_new_wide(const char *hdr_sep)
rec->list = util_list_new(struct util_rec_fld, node);
rec->fmt.type = REC_FMT_WIDE;
rec->fmt.d.wide_p.hdr_sep = util_strdup(hdr_sep);
rec->fmt.d.wide_p.hdr_sep = hdr_sep ? util_strdup(hdr_sep) : NULL;
rec->fmt.d.wide_p.argz_sep = ',';
rec->fmt.indent = 0;
return rec;
@@ -249,7 +249,7 @@ static void rec_free_wide(struct util_rec *rec)
/**
* Create a new record with "long" output format
*
* @param[in] hdr_sep Header separator
* @param[in] hdr_sep Header separator or %NULL for no separator
* @param[in] col_sep Column separator
* @param[in] key Primary key of record
* @param[in] key_size Width of left column i.e. keys
@@ -264,7 +264,7 @@ struct util_rec *util_rec_new_long(const char *hdr_sep, const char *col_sep,
rec->list = util_list_new(struct util_rec_fld, node);
rec->fmt.type = REC_FMT_LONG;
rec->fmt.d.long_p.hdr_sep = util_strdup(hdr_sep);
rec->fmt.d.long_p.hdr_sep = hdr_sep ? util_strdup(hdr_sep) : NULL;
rec->fmt.d.long_p.col_sep = util_strdup(col_sep);
rec->fmt.d.long_p.key = util_strdup(key);
rec->fmt.d.long_p.key_size = key_size;

View File

@@ -45,7 +45,7 @@ vmcmd: Trigger CP command according to the 'VMCMD_X' configuration in
.TP
\fB - DUMP_TYPE:\fR
Type of dump device. Possible values are 'ccw', 'fcp' and 'nvme'.
Type of dump device. Possible values are 'ccw', 'eckd', 'fcp' and 'nvme'.
.TP
\fB - DEVICE:\fR
@@ -71,6 +71,11 @@ Namespace ID for NVMe dump device.
\fB - BOOTPROG:\fR
Boot program selector.
.TP
\fB - BR_CHR:\fR
Boot record location in "C,H,R" format (comma separated values for
Cylinder, Head and Record) or "auto".
.TP
\fB - BR_LBA:\fR
Boot record logical block address.
@@ -146,6 +151,23 @@ DEVICE=0.0.1234
DELAY_MINUTES=5
.br
#
.br
# Example configuration for an ECKD dump device (DASD)
.br
#
.br
ON_PANIC=dump
.br
DUMP_TYPE=eckd
.br
DEVICE=0.0.1004
.br
BOOTPROG=0
.br
BR_CHR=auto
.br
#
.br
# Example configuration for an FCP dump device (SCSI Disk)

View File

@@ -1,22 +0,0 @@
# Common definitions
include ../common.mak
.DEFAULT_GOAL := all
PKGDATADIR := "$(DESTDIR)$(TOOLS_DATADIR)/pvattest"
SUBDIRS := src man tools
RECURSIVE_TARGETS := all-recursive clean-recursive install-recursive
all: all-recursive
install: install-recursive
clean: clean-recursive
$(RECURSIVE_TARGETS):
@target=`echo $@ |sed s/-recursive//`; \
for d in $(SUBDIRS); do \
$(MAKE) -C $$d $$target || exit 1; \
done
.PHONY: all install clean $(RECURSIVE_TARGETS)

View File

@@ -1,100 +0,0 @@
# pvattest
Use `pvattest` to attest an IBM Secure Execution guest running on z16 and later.
With `pvattest` you can create attestation requests in a trusted environment and attest
an IBM Secure Execution for Linux guest to verify that a provider is running the correct image.
To achieve this, use the following commands:
* `create` On a trusted system, creates an attestation request.
* `perform` Performs an attestation measurement on the SE-guest to be attested. For this a
attestation request is sent to the Ultravisor (UV) and the answer received. The `perform`
command requires IBM z16 or later z/Architecture hardware.
* `verify` On a trusted system, compares the answer from the Ultravisor to the
expected answer. If they differ, the Secure Execution guest might be a different guest
than expected, or not secure at all.
For meaningful results, run `create` and `verify` only in a trusted environment,
like your workstation or a previously attested IBM Secure Execution guest.
Otherwise, the attestation can be compromised.
For all certificates, revocation lists, and host-key documents, both the PEM and DER input
formats are supported. If you run this program on a non S390 System, 'perform' is not be available.
## Getting started
If all dependencies are met (see the s390-tools README) issue `make` in the source tree to build `pvattest`.
## Details
### create
`pvattest create` needs the host-key-document, a location to store the
attestation request protection key, and a location to store the request data.
Unless the `--no-verify` flag is set it additionally requires the IBM signing key
and the intermediate CA. The output contains the request in binary form which serves as input
to `pvattest perform`. Must be run in a trusted environment. Especially, do not create the request
on a system you want to attest. The attestation request protection key is valid for this request only,
must be kept until the verification is completed and must be destroyed afterwards
Keep the key secret.
### perform
`pvattest perform` needs a request in binary form generated by `pvattest create`and
a location to store the output. It will send the request to the device at `/dev/uv`
which passes the request to the Ultravisor.
Kernel will then send the request to the Ultravisor which will calculate the answer.
The Answer is then passed back to userspace and handled by `pvattest`
The output includes the original request and the answer from the Ultravisor.
### verify
`pvattest verify` needs the SE-guest header, the attestation request protection key,
and the attestation request and the response to the `pvattest perform` command from the Ultravisor.
It calculates the measurement in the trusted environment and compares it to the response from
the Ultravisor in the previous step.
The following return codes are possible:
0. successful verification: The calculated measurement matches the response from the Ultravisor
1. failed verification: The command ended with an error, for example, because of incorrect input or an invalid SE header
2. failed verification: The calculated measurement does not match the response from the Ultravisor
Run `pvattest verify` in a trusted environment. Especially, do not verify on the system you want to attest.
## Measurement
The measurement is a cryptographic measurement of the following block.
Only HMAC-SHA512 is supported.
| Start | Size | Content |
|---------|------------|---------------------------------------------------------------|
| 0x0 | 0x40 | Page List Digest (from SE header) |
| 0x40 | 0x40 | Address List Digest (from SE header) |
| 0x80 | 0x40 | Tweak List Digest (from SE header) |
| 0xc0 | 0x10 | SE Header Tag (from SE header) |
| 0xd0 | 0x10 | Configuration UID (generated by UV, included in the answer) |
| 0xe0 | 0x02 | User Data Length (defined during measurement on the SE-guest) |
| 0xe2 | 0x02 | Zeros |
| 0xe4 | 0x04 | Additional Data Length (set by UV, included in the answer) |
| 0xe8 | 0 - 0x100 | User Data (generated during measurement on the SE-guest) |
| ... | 0 or 0x10 | Optional Nonce (generated during request creation) |
| ... | 0 - 0x8000 | Additional Data (generated by UV, included in the answer) |
### User Data
By default `pvattest` does not include any User Data, therefore the length is zero.
`User Data` is data generated by the SE guest and passed to UV during the measurement.
The `User Data` must be known to or be replicable by the verifier to verify the correctness of the User Data.
The addition of user data is currently an experimental setting.
### Additional Data
`Additional data` is data known to the Ultravisor. By default UV will not include any `Additional Data`.
Adding `Additional Data` is currently an experimental setting.
## Example
Create an attestation request in a trusted environment:
`pvattest create -k hkd.crt --arpk arp.key -o arcb.bin --cert IntermediateCA.crt --cert IbmSigningKey.crt`
Perform an attestation measurement on an IBM Secure Execution guest:
`pvattest perform --input arcb.bin --output measurement.bin`
Verify the response from the Ultravisor against the attestation request in a trusted environment:
`pvattest verify --input measurement.bin --arpk arp.key --hdr se_guest.hdr`

View File

@@ -1,9 +0,0 @@
include ../../common.mak
all:
install:
$(INSTALL) -d -m 755 $(DESTDIR)$(MANDIR)/man1
$(INSTALL) -m 644 -c *.1 -t $(DESTDIR)$(MANDIR)/man1
.PHONY: all install clean

View File

@@ -1,83 +0,0 @@
.\" Copyright 2022 IBM Corp.
.\" s390-tools is free software; you can redistribute it and/or modify
.\" it under the terms of the MIT license. See LICENSE for details.
.\"
.TH pvattest-create 1 "07 June 2022" "s390-tools" "Attestation Manual"
.nh
.ad l
.SH NAME
\fBpvattest [OPTION?] create [OPTIONS] \fP- create an attestation measurement request
\fB
.SH DESCRIPTION
Prepare attestation measurement requests for an IBM Secure Execution guest.
Only prepare attestation requests in a trusted environment, such as your workstation.
The 'pvattest create' command creates a randomly generated key to protect the attestation request.
This key is only valid for this specific request. In order to avoid compromising the attestation,
do not publish the protection key and delete it after verification.
Every 'create' command generates a new, random protection key.
.SH OPTIONS
.TP
.B
\fB-h\fP, \fB--help\fP
Prints usage information, then exits.
.TP
.B
\fB-k\fP, \fB--host-key-document\fP=\fBFILE\fP
Specify one or more host key documents. At least one is required.
Specify this option multiple times to create an attestation request control block that is usable on multiple hosts.
.TP
.B
\fB-C\fP, \fB--cert\fP=\fBFILE\fP
Specifies the certificate that is used to establish a chain of trust for the verification of the host-key documents. Specify this option twice to specify the IBM Z signing key and the intermediate CA certificate (signed by the root CA). Required. Ignored when \fB--no-verify\fP is specified.
.TP
.B
\fB--crl\fP=\fBFILE\fP
Specifies the revocation list that is used to check whether a certificate of the chain of trust is
revoked. Specify this option multiple times to use multiple CRLs (optional).
.TP
.B
\fB--root-ca\fP=\fBFILE\fP
Specifies the root CA certificate for the verification. If omitted,
the system wide root CAs installed on the system are used. Use
this only if you trust the specified certificate. Optional.
.TP
.B
\fB-o\fP, \fB--output\fP=\fBFILE\fP
\fBFILE\fP specifies the output for the attestation request control block.
.TP
.B
\fB-a\fP, \fB--arpk\fP=\fBFILE\fP
Save the protection key as GCM-AES256 key in \fBFILE\fP Do not publish this key, otherwise your attestation is compromised.
.TP
.B
\fB--no-verify\fP
Disable the host-key document verification. Does not require the host-key documents to be valid. Do
not use for a production request unless you verified the host-key document before (optional).
.TP
.B
\fB--offline\fP
Specifies offline mode, in which no attempt is made to download CRLs. (optional).
.TP
.B
\fB-V\fP, \fB--verbose\fP
Provide more detailed output (optional).
.SH EXAMPLE
Create an attestation request with the protection key 'arp.key', write the request to 'arcb.bin', and verify the host-key document using the CA-signed key 'DigiCertCA.crt' and the intermediate key 'IbmSigningKey.crt'.
.PP
.nf
.fam C
pvattest create -k hkd.crt --arpk arp.key -o attreq.bin --cert DigiCertCA.crt --cert IbmSigningKey.crt
.fam T
.fi
Create an attestation request with the protection key 'arp.key', write the request to 'arcb.bin', verify the host-key document using the CA-signed key 'DigiCertCA.crt' and the intermediate key 'IbmSigningKey.crt', and instead of downloading the certificate revocation list use certificate revocation lists 'DigiCertCA.crl', 'IbmSigningKey.crl', and 'rootCA.crl'.
.PP
.nf
.fam C
pvattest create -k hkd.crt --arpk arp.key -o attreq.bin --cert DigiCertCA.crt --cert IbmSigningKey.crt --offline --crl DigiCertCA.crl --crl IbmSigningKey.crl --crl rootCA.crl
.fam T
.fi
.SH SEE ALSO
\fBpvattest\fP(1), \fBpvattest-verify\fP(1), \fBpvattest-perform\fP(1)

View File

@@ -1,50 +0,0 @@
.\" Copyright 2022 IBM Corp.
.\" s390-tools is free software; you can redistribute it and/or modify
.\" it under the terms of the MIT license. See LICENSE for details.
.\"
.TH pvattest-perform 1 "07 June 2022" "s390-tools" "Attestation Manual"
.nh
.ad l
.SH NAME
\fBpvattest [OPTION?] perform [OPTIONS] \fP- execute an attestation measurement request
\fB
.SH DESCRIPTION
Run a measurement of this system using '/dev/uv'. Works only if this device is
available and the attestation Ultravisor facility is present.
The input must be an attestation request created with 'pvattest create'.
Output will contain the original request and the response from the Ultravisor.
.RE
.PP
.SH OPTIONS
.TP
.B
\fB-h\fP, \fB--help\fP
Show help options
.TP
.B
\fB-i\fP, \fB--input\fP=\fBFILE\fP
\fBFILE\fP specifies the attestation request as input.
.TP
.B
\fB-o\fP, \fB--output\fP=\fBFILE\fP
\fBFILE\fP specifies the output for the attestation result.
.TP
.B
\fB-V\fP, \fB--verbose\fP
Provide more detailed output (optional)
.RE
.PP
.SH EXAMPLE
Perform an attestation measurement with the attestation request 'arcb.bin' and write the output to 'measurement.bin'.
.PP
.nf
.fam C
pvattest perform --input attreq.bin --output attresp.bin
.fam T
.fi
.SH SEE ALSO
\fBpvattest\fP(1), \fBpvattest-create\fP(1), \fBpvattest-verify\fP(1)

View File

@@ -1,75 +0,0 @@
.\" Copyright 2022 IBM Corp.
.\" s390-tools is free software; you can redistribute it and/or modify
.\" it under the terms of the MIT license. See LICENSE for details.
.\"
.TH pvattest-verify 1 "07 June 2022" "s390-tools" "Attestation Manual"
.nh
.ad l
.SH NAME
\fBpvattest [OPTION?] verify [OPTIONS] \fP- verify an attestation measurement
\fB
.SH DESCRIPTION
Verify that a previously generated attestation measurement of an IBM Secure Execution guest is as expected. Only verify attestation requests in a trusted environment, such as your workstation. Input must contain the response as produced by 'pvattest perform'. The protection key must be the one that was used to create the request by 'pvattest create'. Please delete it after verification. The header must be the IBM Secure Execution header of the image that was attested during 'pvattest perform'
.RE
.PP
.SH OPTIONS
.TP
.B
\fB-h\fP, \fB--help\fP
Show help options
.TP
.B
\fB-i\fP, \fB--input\fP=\fBFILE\fP
\fBFILE\fP specifies the attestation result as input.
.TP
.B
\fB-o\fP, \fB--ouput\fP=\fBFILE\fP
\fBFILE\fP specifies the output for the verification result.
.TP
.B
\fB--hdr\fP=\fBFILE\fP
Specify the header of the guest image. Exactly one is required.
.TP
.B
\fB-a\fP, \fB--arpk\fP=\fBFILE\fP
Use \fBFILE\fP to specify the GCM-AES256 key to decrypt the attestation request. Delete this key after verification.
.TP
.B
\fB--format\fP=\fByaml\fP
Define the output format.
Default value: 'yaml'
Possible values:
.RS 4
- \fByaml\fP: Use YAML format
.RE
.TP
.B
\fB-V\fP, \fB--verbose\fP
Provide more detailed output (optional)
.RE
.PP
.SH EXAMPLE
To verify a measurement in 'measurement.bin' with the protection key 'arp.kep' and SE-guest header 'se_guest.hdr'.
.PP
.nf
.fam C
pvattest verify --input attresp.bin --arpk arp.key --hdr se_guest.hdr
.fam T
.fi
If the verification was successful the program exists with zero.
If the verification failed it exists with 2 and prints the following to stderr:
.PP
.nf
.fam C
ERROR: Attestation measurement verification failed:
Calculated and received attestation measurement are not the same.
.fam T
.fi
.SH SEE ALSO
\fBpvattest\fP(1), \fBpvattest-create\fP(1), \fBpvattest-perform\fP(1)

View File

@@ -1,104 +0,0 @@
.\" Copyright 2022 IBM Corp.
.\" s390-tools is free software; you can redistribute it and/or modify
.\" it under the terms of the MIT license. See LICENSE for details.
.\"
.TH pvattest 1 "07 June 2022" "s390-tools" "Attestation Manual"
.nh
.ad l
.SH NAME
\fBpvattest [OPTION?] COMMAND [OPTIONS] \fP- create, perform, and verify attestation measurements
\fB
.RE
\fB
.SH SYNOPSIS
.nf
.fam C
\fBpvattest\fP \fIcreate\fP [\fIOPTIONS\fP]
\fBpvattest\fP \fIperform\fP [\fIOPTIONS\fP]
\fBpvattest\fP \fIverify\fP [\fIOPTIONS\fP]
.fam T
.fi
.fam T
.fi
.SH DESCRIPTION
Use \fBpvattest\fP to attest that an IBM Secure Execution guest is the correct guest, and that it was started in a secure manner.
Run '\fBpvattest\fP \fIcreate\fP' and '\fBpvattest\fP \fIverify\fP' in a trusted environment only.
.PP
.nf
.fam C
create On a trusted system, creates an attestation request.
perform On the SE-guest to be attested, sends the attestation request to the Ultravisor and receives the answer.
verify On a trusted system, compares the answer from the Ultravisor to the one from your trusted environment. If they differ, the Secure Execution guest might be compromised.
.fam T
.fi
For meaningful results, run '\fIcreate\fP' and '\fIverify\fP' in a trusted environment, like your workstation or a previously attested IBM Secure Execution guest. Otherwise, the attestation might be tampered with. For all certificates, revocation lists, and host-key documents, both the PEM and DER input formats are supported. If you run \fBpvattest\fP on a machine architecture other than z/Architecture, 'measure' is not available.
.PP
Use '\fBpvattest\fP [COMMAND] \fB-h\fP' to get detailed help
.RE
.PP
.SH OPTIONS
.TP
.B
\fB-h\fP, \fB--help\fP
Show help options
.TP
.B
\fB-v\fP, \fB--version\fP
Print the version and exit.
.TP
.B
\fB-V\fP, \fB--verbose\fP
Provide more detailed output (optional)
.RE
.PP
.SH EXAMPLE
For details refer to the man page of the command.
.PP
Create the request on a trusted system.
.PP
.nf
.fam C
trusted:~$ pvattest create -k hkd.crt --cert CA.crt --cert ibmsk.crt --arpk arp.key -o attreq.bin
.fam T
.fi
On the SE-guest, \fIperform\fP the attestation.
.PP
.nf
.fam C
seguest:~$ pvattest perform -i attreq.bin -o attresp.bin
.fam T
.fi
On a trusted system, \fIverify\fP that the response is correct. Here, the protection key from the creation and the SE-guests header is used to \fIverify\fP the measurement.
.PP
.nf
.fam C
trusted:~$ pvattest verify -i attresp.bin --arpk arp.key --hdr se_guest.hdr
trusted:~$ echo $?
0
.fam T
.fi
If the measurements do not match \fBpvattest\fP exits with code 2 and emits an error message. The SE-guest attestation failed.
.PP
.nf
.fam C
trusted:~$ pvattest verify -i wrongresp.bin --arpk arp.key --hdr se_guest.hdr
ERROR: Attestation measurement verification failed:
Calculated and received attestation measurement are not the same.
trusted:~$ echo $?
2
.fam T
.fi
.SH SEE ALSO
\fBpvattest\fP-\fIcreate\fP(1), \fBpvattest-\fIverify\fP\fP(1), \fBpvattest\fP-\fIperform\fP(1)

View File

@@ -1,2 +0,0 @@
.check-dep-pvattest
.detect-openssl.dep.c

View File

@@ -1,112 +0,0 @@
include ../../common.mak
BIN_PROGRAM = pvattest
PKGDATADIR ?= "$(DESTDIR)$(TOOLS_DATADIR)/$(BIN_PROGRAM)"
SRC_DIR := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
PVATTESTDIR := $(rootdir)/pvattest
INCLUDE_PATHS = "$(SRC_DIR)" "$(rootdir)/include"
INCLUDE_PARMS = $(addprefix -I,$(INCLUDE_PATHS))
LIBPV_DIR = $(rootdir)/libpv
LIBPV = $(LIBPV_DIR)/libpv.a
WARNINGS := -Wall -Wextra -Wshadow \
-Wcast-align -Wwrite-strings -Wmissing-prototypes \
-Wmissing-declarations -Wredundant-decls -Wnested-externs \
-Wno-long-long -Wuninitialized -Wconversion -Wstrict-prototypes \
-Wpointer-arith -Wno-error=inline \
-Wno-unused-function -Wno-unused-parameter -Wno-unused-variable \
$(NULL)
PVATTEST_SRCS := $(wildcard *.c) \
$(NULL)
$(BIN_PROGRAM)_SRCS := \
$(PVATTEST_SRCS) \
$(NULL)
$(BIN_PROGRAM)_OBJS := $($(BIN_PROGRAM)_SRCS:.c=.o)
GLIB2_CFLAGS := $(shell $(PKG_CONFIG) --silence-errors --cflags glib-2.0)
GLIB2_LIBS := $(shell $(PKG_CONFIG) --silence-errors --libs glib-2.0)
LIBCRYPTO_CFLAGS := $(shell $(PKG_CONFIG) --silence-errors --cflags libcrypto)
LIBCRYPTO_LIBS := $(shell $(PKG_CONFIG) --silence-errors --libs libcrypto)
LIBCURL_CFLAGS := $(shell $(PKG_CONFIG) --silence-errors --cflags libcurl)
LIBCURL_LIBS := $(shell $(PKG_CONFIG) --silence-errors --libs libcurl)
ALL_CFLAGS += -DPKGDATADIR=$(PKGDATADIR) \
-DOPENSSL_API_COMPAT=0x10101000L \
$(GLIB2_CFLAGS) \
$(LIBCRYPTO_CFLAGS) \
$(LIBCURL_CFLAGS) \
$(WARNINGS) \
$(NULL)
ifneq ($(call check_header_prereq,"asm/uvdevice.h"),yes)
ALL_CFLAGS += -DPVATTEST_NO_PERFORM
endif
ALL_CPPFLAGS += $(INCLUDE_PARMS)
LDLIBS += $(GLIB2_LIBS) $(LIBCRYPTO_LIBS) $(LIBCURL_LIBS)
BUILD_TARGETS := skip-$(BIN_PROGRAM)
INSTALL_TARGETS := skip-$(BIN_PROGRAM)
ifneq (${HAVE_OPENSSL},0)
ifneq (${HAVE_GLIB2},0)
ifneq (${HAVE_LIBCURL}, 0)
BUILD_TARGETS := $(BIN_PROGRAM)
INSTALL_TARGETS := install-$(BIN_PROGRAM)
endif
endif
endif
all: $(BUILD_TARGETS)
install: $(INSTALL_TARGETS)
$(BIN_PROGRAM): $($(BIN_PROGRAM)_OBJS) $(LIBPV)
skip-$(BIN_PROGRAM):
echo " SKIP $(BIN_PROGRAM) due to unresolved dependencies"
clean:
$(RM) -f -- $($(BIN_PROGRAM)_OBJS) $(BIN_PROGRAM) .check-dep-$(BIN_PROGRAM) .detect-openssl.dep.c
install-$(BIN_PROGRAM): $(BIN_PROGRAM)
$(INSTALL) -d -m 755 $(DESTDIR)$(USRBINDIR)
$(INSTALL) -c $^ $(DESTDIR)$(USRBINDIR)
.PHONY: all install clean skip-$(BIN_PROGRAM) install-$(BIN_PROGRAM)
$($(BIN_PROGRAM)_OBJS): .check-dep-$(BIN_PROGRAM)
.detect-openssl.dep.c:
echo "#include <openssl/evp.h>" > $@
echo "#if OPENSSL_VERSION_NUMBER < 0x10101000L" >> $@
echo " #error openssl version 1.1.1 is required" >> $@
echo "#endif" >> $@
echo "static void __attribute__((unused)) test(void) {" >> $@
echo " EVP_MD_CTX *ctx = EVP_MD_CTX_new();" >> $@
echo " EVP_MD_CTX_free(ctx);" >> $@
echo "}" >> $@
.check-dep-$(BIN_PROGRAM): .detect-openssl.dep.c
$(call check_dep, \
"$(BIN_PROGRAM)", \
"glib.h", \
"glib2-devel / libglib2.0-dev", \
"HAVE_GLIB2=0")
$(call check_dep, \
"$(BIN_PROGRAM)", \
$^, \
"openssl-devel / libssl-dev version >= 1.1.1", \
"HAVE_OPENSSL=0", \
"-I.")
$(call check_dep, \
"$(BIN_PROGRAM)", \
"curl/curl.h", \
"libcurl-devel", \
"HAVE_LIBCURL=0")
touch $@

View File

@@ -1,423 +0,0 @@
/*
* Attestation Request Control Block related functions
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include <openssl/evp.h>
#include <stdlib.h>
#include "libpv/crypto.h"
#include "libpv/hash.h"
#include "arcb.h"
#include "common.h"
#include "log.h"
#define ARVN_VERSION_1 0x0100
#define MAX_ARL 0x2000
typedef struct arcb_v1_hdr {
uint64_t reserved0; /* 0x0000 */
be32_t arvn; /* 0x0008 */
be32_t arl; /* 0x000c */
uint8_t iv[ARCB_V1_IV_SIZE]; /* 0x0010 */
uint32_t reserved1c; /* 0x001c */
uint8_t reserved20[7]; /* 0x0020 */
uint8_t nks; /* 0x0027 */
uint32_t reserved28; /* 0x0028 */
be32_t sea; /* 0x002c */
be64_t paf; /* 0x0030 */
be32_t mai; /* 0x0038 */
uint32_t reserved3c; /* 0x003c */
PvEcdhPubKey cpk; /* 0x0040 */
} __packed arcb_v1_hdr_t;
G_STATIC_ASSERT(sizeof(arcb_v1_hdr_t) == 0xe0);
typedef struct arcb_v1_key_slot {
uint8_t phkh[ARCB_V1_PHKH_SIZE];
uint8_t warpk[ARCB_V1_ATTEST_PROT_KEY_SIZE];
uint8_t kst[ARCB_V1_TAG_SIZE];
} __packed arcb_v1_key_slot_t;
G_STATIC_ASSERT(sizeof(arcb_v1_key_slot_t) == 0x50);
struct arcb_v1 {
/* authenticated data */
uint32_t arvn;
uint32_t mai;
uint64_t paf;
GBytes *iv;
EVP_PKEY *evp_cust_pub_key;
GSList *host_key_slots;
/* confidential data */
GBytes *confidential_measurement_key;
GBytes *confidential_optional_nonce;
GBytes *confidential_att_req_prot_key;
};
void arcb_v1_clear_free(arcb_v1_t *arcb)
{
if (!arcb)
return;
g_slist_free_full(arcb->host_key_slots, g_free);
g_bytes_unref(arcb->confidential_measurement_key);
g_bytes_unref(arcb->confidential_optional_nonce);
g_bytes_unref(arcb->confidential_att_req_prot_key);
g_bytes_unref(arcb->iv);
EVP_PKEY_free(arcb->evp_cust_pub_key);
g_free(arcb);
}
static void arcb_v1_set_paf(arcb_v1_t *arcb, const uint64_t paf, GError **error)
{
const uint64_t known_flags = ARCB_V1_PAF_ALL & ~ARCB_V1_PAF_NONCE;
if ((paf & ARCB_V1_PAF_NONCE) != 0) {
g_set_error(error, ARCB_ERROR, ARCB_ERR_INVALID_PAF,
_("The given paf (%#.16lx) specifies the NONCE flag (%#.16lx)."), paf,
ARCB_V1_PAF_NONCE);
return;
}
if ((paf & ~known_flags) != 0)
pvattest_log_warning(
_("The given paf (%#.16lx) specifies unknown flags. Use at your own risk!"),
paf, known_flags);
arcb->paf = paf;
}
arcb_v1_t *arcb_v1_new(GBytes *arpk, GBytes *iv, uint32_t mai, EVP_PKEY *evp_cpk, GBytes *mkey,
uint64_t paf, GError **error)
{
g_autoptr(arcb_v1_t) arcb = g_new0(arcb_v1_t, 1);
g_assert(g_bytes_get_size(iv) == ARCB_V1_IV_SIZE);
g_assert(g_bytes_get_size(arpk) == ARCB_V1_ATTEST_PROT_KEY_SIZE);
g_assert(g_bytes_get_size(mkey) == HMAC_SHA512_KEY_SIZE);
pv_wrapped_g_assert(arpk);
pv_wrapped_g_assert(iv);
pv_wrapped_g_assert(evp_cpk);
pv_wrapped_g_assert(mkey);
arcb->arvn = ARVN_VERSION_1;
arcb->mai = mai;
arcb_v1_set_paf(arcb, paf, error);
if (*error)
return NULL;
arcb->iv = g_bytes_ref(iv);
if (EVP_PKEY_up_ref(evp_cpk) != 1)
g_abort();
arcb->evp_cust_pub_key = evp_cpk;
arcb->confidential_att_req_prot_key = g_bytes_ref(arpk);
arcb->confidential_measurement_key = g_bytes_ref(mkey);
return g_steal_pointer(&arcb);
}
int arcb_v1_add_key_slot(arcb_v1_t *arcb, EVP_PKEY *evp_host, GError **error)
{
g_autoptr(GBytes) warpk = NULL, tag = NULL, phkh = NULL;
g_autoptr(GBytes) exchange_key = NULL, iv = NULL;
g_autofree arcb_v1_key_slot_t *key_slot = NULL;
g_autofree PvEcdhPubKey *ecdh_host = NULL;
g_autofree uint8_t *iv_raw = NULL;
PvCipherParms parms;
int64_t gcm_rc;
g_assert(arcb->confidential_att_req_prot_key);
pv_wrapped_g_assert(arcb);
pv_wrapped_g_assert(evp_host);
/* encrypt (=wrap) attestation request protection key, store warpk + tag */
exchange_key = pv_derive_exchange_key(arcb->evp_cust_pub_key, evp_host, error);
if (!exchange_key)
return -1;
iv_raw = g_malloc0(ARCB_V1_IV_SIZE);
iv = g_bytes_new_take(g_steal_pointer(&iv_raw), ARCB_V1_IV_SIZE);
if (!iv)
g_abort();
parms.key = exchange_key;
parms.iv = iv;
parms.cipher = EVP_aes_256_gcm();
parms.tag_size = ARCB_V1_TAG_SIZE;
gcm_rc = pv_gcm_encrypt(arcb->confidential_att_req_prot_key, NULL, &parms, &warpk, &tag,
error);
if (gcm_rc != ARCB_V1_ATTEST_PROT_KEY_SIZE)
return -1;
/* calculate public host key hash */
ecdh_host = pv_evp_pkey_to_ecdh_pub_key(evp_host, error);
if (!ecdh_host)
return -1;
phkh = pv_sha256_hash(ecdh_host->data, sizeof(ecdh_host->data), error);
if (!phkh)
return -1;
/* copy to list */
g_assert(g_bytes_get_size(warpk) == sizeof(key_slot->warpk));
g_assert(g_bytes_get_size(tag) == sizeof(key_slot->kst));
g_assert(g_bytes_get_size(phkh) == sizeof(key_slot->phkh));
key_slot = g_malloc0(sizeof(*key_slot));
pv_gbytes_memcpy(key_slot->warpk, sizeof(key_slot->warpk), warpk, NULL);
pv_gbytes_memcpy(key_slot->kst, sizeof(key_slot->warpk), tag, NULL);
pv_gbytes_memcpy(key_slot->phkh, sizeof(key_slot->warpk), phkh, NULL);
arcb->host_key_slots = g_slist_prepend(arcb->host_key_slots, g_steal_pointer(&key_slot));
return 0;
}
void arcb_v1_set_nonce(arcb_v1_t *arcb, GBytes *nonce)
{
pv_wrapped_g_assert(arcb);
pv_wrapped_g_assert(nonce);
arcb_v1_rm_nonce(arcb);
g_assert(!arcb->confidential_optional_nonce);
g_assert(g_bytes_get_size(nonce) == ARCB_V1_NONCE_SIZE);
arcb->confidential_optional_nonce = g_bytes_ref(nonce);
arcb->paf |= ARCB_V1_PAF_NONCE;
}
void arcb_v1_rm_nonce(arcb_v1_t *arcb)
{
pv_wrapped_g_assert(arcb);
if (!arcb->confidential_optional_nonce)
return;
g_bytes_unref(arcb->confidential_optional_nonce);
arcb->confidential_optional_nonce = NULL;
arcb->paf &= ~ARCB_V1_PAF_NONCE;
}
GBytes *arcb_v1_serialize(const arcb_v1_t *arcb, GError **error)
{
pv_wrapped_g_assert(arcb);
g_autoptr(GByteArray) arcb_gba = NULL;
g_autoptr(GBytes) confidential_area = NULL;
g_autoptr(GBytes) aad = NULL;
g_autoptr(GBytes) art = NULL;
g_autoptr(GBytes) encrypted_area = NULL;
g_autoptr(GBytes) result = NULL;
g_autofree PvEcdhPubKey *ecdh_cpk = NULL;
PvCipherParms parms = {
.cipher = EVP_aes_256_gcm(),
.tag_size = AES_256_GCM_TAG_SIZE,
};
size_t att_req_len = 0, nks = 0, sea = 0;
arcb_v1_hdr_t hdr = {
.arvn = GUINT32_TO_BE(arcb->arvn),
.paf = GUINT64_TO_BE(arcb->paf),
.mai = GUINT32_TO_BE(arcb->mai),
};
g_assert(arcb->host_key_slots);
/* calculate sizes */
nks = g_slist_length(arcb->host_key_slots);
g_assert(nks < 0xFF);
sea = g_bytes_get_size(arcb->confidential_measurement_key);
if (arcb->confidential_optional_nonce)
sea += g_bytes_get_size(arcb->confidential_optional_nonce);
g_assert(sea == HMAC_SHA512_KEY_SIZE || sea == HMAC_SHA512_KEY_SIZE + ARCB_V1_NONCE_SIZE);
att_req_len = sizeof(hdr) + nks * sizeof(arcb_v1_key_slot_t) + HMAC_SHA512_KEY_SIZE +
ARCB_V1_TAG_SIZE;
if (arcb->confidential_optional_nonce)
att_req_len += ARCB_V1_NONCE_SIZE;
g_assert(att_req_len <= MAX_ARL);
/* copy plain data to contiguous memory */
hdr.arl = GUINT32_TO_BE((uint32_t)att_req_len);
pv_gbytes_memcpy(hdr.iv, ARCB_V1_IV_SIZE, arcb->iv, NULL);
hdr.nks = (uint8_t)nks;
hdr.sea = GUINT32_TO_BE((uint32_t)sea);
ecdh_cpk = pv_evp_pkey_to_ecdh_pub_key(arcb->evp_cust_pub_key, error);
memcpy(&hdr.cpk, ecdh_cpk, sizeof(*ecdh_cpk));
arcb_gba = g_byte_array_sized_new((guint)att_req_len);
g_byte_array_append(arcb_gba, (const uint8_t *)&hdr, sizeof(hdr));
for (GSList *elem = arcb->host_key_slots; elem; elem = elem->next)
g_byte_array_append(arcb_gba, elem->data, sizeof(arcb_v1_key_slot_t));
/* encrypt the confidential data */
confidential_area = secure_gbytes_concat(arcb->confidential_measurement_key,
arcb->confidential_optional_nonce);
parms.key = arcb->confidential_att_req_prot_key;
parms.iv = arcb->iv;
aad = g_bytes_new(arcb_gba->data, arcb_gba->len);
pv_gcm_encrypt(confidential_area, aad, &parms, &encrypted_area, &art, error);
if (*error)
return NULL;
g_byte_array_append(arcb_gba, g_bytes_get_data(encrypted_area, NULL), (guint)sea);
g_byte_array_append(arcb_gba, g_bytes_get_data(art, NULL), ARCB_V1_TAG_SIZE);
result = g_byte_array_free_to_bytes(arcb_gba);
arcb_gba = NULL;
return g_steal_pointer(&result);
}
uint32_t arcb_v1_get_required_measurement_size(const arcb_v1_t *arcb, GError **error)
{
pv_wrapped_g_assert(arcb);
switch (arcb->mai) {
case MAI_HMAC_SHA512:
return HMAC_SHA512_KEY_SIZE;
default:
g_set_error(error, ARCB_ERROR, ARCB_ERR_INVALID_MAI,
_("Unknown measurement algorithm ID specified (%#x)."), arcb->mai);
return 0;
}
}
uint32_t arcb_v1_get_required_additional_size(const arcb_v1_t *arcb)
{
uint32_t size = 0;
pv_wrapped_g_assert(arcb);
if (arcb_v1_additional_has_phkh_image(arcb))
size += ARCB_V1_PHKH_SIZE;
if (arcb_v1_additional_has_phkh_attest(arcb))
size += ARCB_V1_PHKH_SIZE;
return size;
}
gboolean arcb_v1_use_nonce(const arcb_v1_t *arcb)
{
pv_wrapped_g_assert(arcb);
return arcb->confidential_optional_nonce != NULL;
}
gboolean arcb_v1_additional_has_phkh_image(const arcb_v1_t *arcb)
{
pv_wrapped_g_assert(arcb);
return (arcb->paf & ARCB_V1_PAF_AAD_PHKH_HEADER) != 0;
}
gboolean arcb_v1_additional_has_phkh_attest(const arcb_v1_t *arcb)
{
pv_wrapped_g_assert(arcb);
return (arcb->paf & ARCB_V1_PAF_AAD_PHKH_ATTEST) != 0;
}
GBytes *arcb_v1_get_measurement_key(const arcb_v1_t *arcb)
{
pv_wrapped_g_assert(arcb);
return g_bytes_ref(arcb->confidential_measurement_key);
}
GBytes *arcb_v1_get_nonce(const arcb_v1_t *arcb)
{
pv_wrapped_g_assert(arcb);
if (arcb->confidential_optional_nonce)
return g_bytes_ref(arcb->confidential_optional_nonce);
return NULL;
}
GBytes *arcb_v1_get_arp_key(const arcb_v1_t *arcb)
{
pv_wrapped_g_assert(arcb);
return g_bytes_ref(arcb->confidential_att_req_prot_key);
}
static gboolean is_v1_arcb(size_t aad_size, size_t sea, size_t arl, size_t serialized_arcb_size,
uint32_t arcb_version, gboolean has_nonce)
{
gboolean result = aad_size + sea + ARCB_V1_TAG_SIZE == arl;
result &= arl <= serialized_arcb_size;
result &= arcb_version == ARVN_VERSION_1;
result &= has_nonce ? sea == HMAC_SHA512_KEY_SIZE + ARCB_V1_NONCE_SIZE :
sea == HMAC_SHA512_KEY_SIZE;
return result;
}
gboolean arcb_v1_verify_serialized_arcb(GBytes *serialized_arcb, GBytes *arpk,
GBytes **measurement_key, GBytes **optional_nonce,
GError **error)
{
g_autoptr(GBytes) encr = NULL, decr = NULL, aad = NULL, tag = NULL, iv = NULL;
const struct arcb_v1_hdr *serialized_arcb_hdr;
const uint8_t *encr_u8, *aad_u8, *tag_u8;
const uint8_t *serialized_arcb_u8;
size_t serialized_arcb_size;
uint32_t arcb_version, mai;
size_t aad_size, arl, sea;
PvCipherParms parms;
gboolean has_nonce;
uint64_t paf;
pv_wrapped_g_assert(serialized_arcb);
pv_wrapped_g_assert(arpk);
serialized_arcb_u8 = g_bytes_get_data(serialized_arcb, &serialized_arcb_size);
serialized_arcb_hdr = (const arcb_v1_hdr_t *)serialized_arcb_u8;
arl = GUINT32_FROM_BE(serialized_arcb_hdr->arl);
arcb_version = GUINT32_FROM_BE(serialized_arcb_hdr->arvn);
mai = GUINT32_FROM_BE(serialized_arcb_hdr->mai);
aad_u8 = serialized_arcb_u8;
aad_size = sizeof(*serialized_arcb_hdr) +
serialized_arcb_hdr->nks * sizeof(arcb_v1_key_slot_t);
encr_u8 = aad_u8 + aad_size;
sea = GUINT32_FROM_BE(serialized_arcb_hdr->sea);
tag_u8 = encr_u8 + sea;
paf = GUINT64_FROM_BE(serialized_arcb_hdr->paf);
has_nonce = (paf & ARCB_V1_PAF_NONCE) != 0;
if (!is_v1_arcb(aad_size, sea, arl, serialized_arcb_size, arcb_version, has_nonce)) {
g_set_error(error, ARCB_ERROR, ARCB_ERR_INVALID_ARCB,
_("The provided attestation request is not valid"));
return FALSE;
}
if (mai != MAI_HMAC_SHA512) {
g_set_error(error, ARCB_ERROR, ARCB_ERR_INVALID_MAI,
_("Unsupported measurement argument ID (%#x)"), mai);
return FALSE;
}
aad = g_bytes_new(aad_u8, aad_size);
encr = g_bytes_new(encr_u8, sea);
tag = g_bytes_new(tag_u8, ARCB_V1_TAG_SIZE);
iv = g_bytes_new(serialized_arcb_hdr->iv, sizeof(serialized_arcb_hdr->iv));
parms.cipher = EVP_aes_256_gcm();
parms.tag_size = AES_256_GCM_TAG_SIZE;
parms.key = arpk;
parms.iv = iv;
pv_gcm_decrypt(encr, aad, tag, &parms, &decr, error);
if (*error) {
GError *tmp_error = NULL;
g_set_error(&tmp_error, ARCB_ERROR, ARCB_ERR_INVALID_ARCB,
_("Cannot verify the attestation request: %s"), (*error)->message);
g_clear_error(error);
g_propagate_error(error, tmp_error);
return FALSE;
}
if (measurement_key)
*measurement_key = g_bytes_new(g_bytes_get_data(decr, NULL), HMAC_SHA512_KEY_SIZE);
if (optional_nonce && has_nonce)
*optional_nonce =
g_bytes_new((uint8_t *)g_bytes_get_data(decr, NULL) + HMAC_SHA512_KEY_SIZE,
ARCB_V1_NONCE_SIZE);
return TRUE;
}

View File

@@ -1,152 +0,0 @@
/*
* Attestation Request Control Block related functions
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef PVATTEST_ARCB_H
#define PVATTEST_ARCB_H
/* Must be included before any other header */
#include "config.h"
#include "libpv/glib-helper.h"
#include "lib/zt_common.h"
#include "libpv/crypto.h"
#include "libpv/macros.h"
#include "types.h"
#define MAI_HMAC_RESERVED_INVALID 0
#define MAI_HMAC_SHA512 0x1
#define HMAC_SHA512_KEY_SIZE 64
#define ARCB_V1_ATTEST_PROT_KEY_SIZE 32
#define ARCB_V1_NONCE_SIZE 16
#define ARCB_V1_TAG_SIZE 16
#define ARCB_V1_IV_SIZE 12
#define ARCB_V1_PHKH_SIZE 32
/* Optional nonce in ARCB */
#define ARCB_V1_PAF_NONCE PV_MSB(1)
/* Public host key hash used to unseal SE header added to additional data to be measured */
#define ARCB_V1_PAF_AAD_PHKH_HEADER PV_MSB(2)
/* Public host key hash used to unseal this attestation added to additional data to be measured */
#define ARCB_V1_PAF_AAD_PHKH_ATTEST PV_MSB(3)
/* Temporary backup-host-key use allowed */
#define ARCB_V1_PAF_TMP_BACKUP_ALLOWED PV_MSB(62)
/* Global not-host-specific key allowed */
#define ARCB_V1_PAF_GLOBAL_NHS_KEY_ALLOWED PV_MSB(63)
#define ARCB_V1_PAF_ALL \
(ARCB_V1_PAF_NONCE | ARCB_V1_PAF_AAD_PHKH_HEADER | ARCB_V1_PAF_AAD_PHKH_ATTEST | \
ARCB_V1_PAF_TMP_BACKUP_ALLOWED | ARCB_V1_PAF_GLOBAL_NHS_KEY_ALLOWED)
typedef struct arcb_v1 arcb_v1_t;
/** arcb_v1_new:
*
* @arpk: Attestation Request Protection key. AES-GCM-256 key to
* protect Measurement Key and Nonce.
* Must be ´ARCB_V1_ATTEST_PROT_KEY_SIZE´ bytes long.
* @iv: IV for protecting Measuremt Key and Nonce.
* Should be random for each new ARPK.
* Must be ´ARCB_V1_IV_SIZE´ bytes long.
* @mai: Measurement Algorithm Identifier for the attestation measurement.
* See ´enum mai´
* @evp_cpk: Customer key in EVP_PKEY format. Must contain private and public key pair.
* @mkey: Measurement key to calculate the Measurement.
* Must be ´HMAC_SHA512_KEY_SIZE´ bytes long.
* @paf: Plain text Attestation Flags. See ´enum plaintext_attestattion_flags´.
* ´ARCB_V1_PAF_NONCE´ must not be set.
* @error: GError. *error will != NULL if error occours.
*
* arpk, mkey, and iv must me correct size
* If not this is considered as a programming error (No warning;
* Results in Assertion or undefined behavior).
*
* GBytes will be ref'ed.
*
* All numbers must be in system byte order and will be converted to big endian
* if needed.
*
* Returns: (nullable) (transfer full): new ARCB context.
*/
arcb_v1_t *arcb_v1_new(GBytes *arpk, GBytes *iv, uint32_t mai, EVP_PKEY *evp_cpk, GBytes *mkey,
uint64_t paf, GError **error) PV_NONNULL(1, 2, 4, 5);
void arcb_v1_clear_free(arcb_v1_t *arcb);
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(arcb_v1_t, arcb_v1_clear_free)
/** arcb_v1_add_key_slot:
*
* @arcb: ARCB context.
* @evp_host: Host public key.
* @error: GError. *error will != NULL if error occours.
*
* Builds a key slot. Calculates exchange key, wraps ARPK with the exchange key.
* Calculates the public host key hash. Calculates the key slot tag.
* Adds it to the ARCB.
*
* Returns: 0 in case of success, -1 otherwise
*/
int arcb_v1_add_key_slot(arcb_v1_t *arcb, EVP_PKEY *evp_host, GError **error) PV_NONNULL(1, 2);
void arcb_v1_set_nonce(arcb_v1_t *arcb, GBytes *nonce) PV_NONNULL(1, 2);
void arcb_v1_rm_nonce(arcb_v1_t *arcb) PV_NONNULL(1);
/** arcb_v1_serialize:
*
* @arcb: ARCB context.
* @error: GError. *error will != NULL if error occurs.
*
* Will create a valid ARCB for the UV. Including encrypting confidential data.
* At least one key_slot must be added beforehand.
*
* Returns: (nullable) (transfer full): The serialized ARCB which can be added to the
* Retrieve Attestation Measurement UVC as GBytes.
*/
GBytes *arcb_v1_serialize(const arcb_v1_t *arcb, GError **error) PV_NONNULL(1, 2);
uint32_t arcb_v1_get_required_measurement_size(const arcb_v1_t *arcb, GError **error)
PV_NONNULL(1, 2);
uint32_t arcb_v1_get_required_additional_size(const arcb_v1_t *arcb) PV_NONNULL(1);
gboolean arcb_v1_use_nonce(const arcb_v1_t *arcb) PV_NONNULL(1);
gboolean arcb_v1_additional_has_phkh_image(const arcb_v1_t *arcb) PV_NONNULL(1);
gboolean arcb_v1_additional_has_phkh_attest(const arcb_v1_t *arcb) PV_NONNULL(1);
GBytes *arcb_v1_get_measurement_key(const arcb_v1_t *arcb) PV_NONNULL(1);
GBytes *arcb_v1_get_nonce(const arcb_v1_t *arcb) PV_NONNULL(1);
GBytes *arcb_v1_get_arp_key(const arcb_v1_t *arcb) PV_NONNULL(1);
/** arcb_v1_verify_serialized_arcb:
*
* @serialized_arcb: binary ARCB in UV readable format.
* @arpk: Attestation Request Protection key that was used to create serialized_arpk
* @measurement_key: Output parameter: decrypted measurement key if no error.
* May be NULL if not interested for this output.
* @optional_nonce: Output parameter: decrypted nonce if no error.
* May be NULL if not interested for this output.
* @error: GError. *error will != NULL if error occurs.
*
*
* Checks if sizes are sound and flags are known by this implementation.
* Decrypts Measurement key and nonce (if given) and verifies ARCB tag.
*
* Returns: TRUE if ARCB is valid, including matching ARCB tag. Otherwise FALSE.
*
*/
gboolean arcb_v1_verify_serialized_arcb(GBytes *serialized_arcb, GBytes *arpk,
GBytes **measurement_key, GBytes **optional_nonce,
GError **error) PV_NONNULL(1, 2);
#define ARCB_ERROR g_quark_from_static_string("pv-arcb_error-quark")
typedef enum arcb_error {
ARCB_ERR_INVALID_ARCB,
ARCB_ERR_INVALID_PAF,
ARCB_ERR_INVALID_MAI,
ARCB_ERR_UNABLE_ENCR_ARPK,
} arcb_error_e;
#endif /* PVATTEST_ARCB_H */

View File

@@ -1,674 +0,0 @@
/*
* Definitions used for parsing arguments.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "argparse.h"
#include "log.h"
#include "common.h"
#define DEFAULT_OUTPUT_FILE_NAME "attest.bin"
#define DEFAULT_OPTION_PHKH_IMG FALSE
#define DEFAULT_OPTION_PHKH_ATT FALSE
#define DEFAULT_OPTION_NO_VERIFY FALSE
#define DEFAULT_OPTION_ONLINE TRUE
#define DEFAULT_OPTION_NONCE TRUE
static pvattest_config_t pvattest_config = {
.general = {
.log_level = PVATTEST_LOG_LVL_DEFAULT,
},
.create = {
.output_path = NULL,
.host_key_document_paths = NULL,
.crl_paths = NULL,
.root_ca_path = NULL,
.certificate_paths = NULL,
.arp_key_out_path = NULL,
.phkh_img = DEFAULT_OPTION_PHKH_IMG,
.phkh_att = DEFAULT_OPTION_PHKH_ATT,
.online = DEFAULT_OPTION_ONLINE,
.use_nonce = DEFAULT_OPTION_NONCE,
.paf = 0,
.x_aad_size = -1,
},
.perform = {
.output_path = NULL,
.input_path = NULL,
},
.verify = {
.input_path = NULL,
.output_path = NULL,
.hdr_path = NULL,
.arp_key_in_path = NULL,
.output_fmt = VERIFY_FMT_YAML,
},
};
typedef gboolean (*verify_options_fn_t)(GError **);
static gboolean check_for_non_null(const void *ptr, const char *msg, GError **error)
{
if (!ptr) {
g_set_error(error, PVATTEST_ERROR, PVATTEST_ERR_INV_ARG, "%s", msg);
return FALSE;
}
return TRUE;
}
static gboolean _check_for_invalid_path(const char *path, gboolean must_exist, GError **error)
{
int cached_errno = 0;
g_assert(path);
if (must_exist) {
if (access(path, F_OK | R_OK) != 0)
cached_errno = errno;
}
if (cached_errno) {
g_set_error(error, PVATTEST_ERROR, PVATTEST_ERR_INV_ARG, "Cannot access '%s': %s",
path, g_strerror(cached_errno));
return FALSE;
}
return TRUE;
}
static gboolean check_for_optional_invalid_path(const char *path, gboolean must_exist,
GError **error)
{
if (!path)
return TRUE;
return _check_for_invalid_path(path, must_exist, error);
}
static gboolean check_for_invalid_path(const char *path, gboolean must_exist, const char *null_msg,
GError **error)
{
if (!check_for_non_null(path, null_msg, error))
return FALSE;
return _check_for_invalid_path(path, must_exist, error);
}
static gboolean _check_file_list(char **path_list, gboolean must_exist, GError **error)
{
char *path = NULL;
for (char **path_it = path_list; path_it != NULL && *path_it != NULL; path_it++) {
path = *path_it;
if (!_check_for_invalid_path(path, must_exist, error))
return FALSE;
}
return TRUE;
}
static gboolean check_optional_file_list(char **path_list, gboolean must_exist, GError **error)
{
if (!path_list)
return TRUE;
return _check_file_list(path_list, must_exist, error);
}
static gboolean check_file_list(char **path_list, gboolean must_exist, const char *null_msg,
GError **error)
{
if (!check_for_non_null(path_list, null_msg, error))
return FALSE;
return _check_file_list(path_list, must_exist, error);
}
static gboolean hex_str_toull(const char *nptr, uint64_t *dst, GError **error)
{
uint64_t value;
gchar *end;
g_assert(dst);
if (!g_str_is_ascii(nptr)) {
g_set_error(
error, PVATTEST_ERROR, PVATTEST_ERR_INV_ARG,
_("Invalid value: '%s'. A hexadecimal value is required, for example '0xcfe'"),
nptr);
return FALSE;
}
value = g_ascii_strtoull(nptr, &end, 16);
if ((value == G_MAXUINT64 && errno == ERANGE) || (end && *end != '\0')) {
g_set_error(
error, PVATTEST_ERROR, PVATTEST_ERR_INV_ARG,
_("Invalid value: '%s'. A hexadecimal value is required, for example '0xcfe'"),
nptr);
return FALSE;
}
*dst = value;
return TRUE;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
/************************* SHARED OPTIONS *************************************/
/* NOTE REQUIRED */
#define _entry_host_key_document(__arg_data, __indent) \
{ \
.long_name = "host-key-document", .short_name = 'k', .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME_ARRAY, .arg_data = __arg_data, \
.description = \
"FILE specifies a host-key document. At least one is required.\n" __indent \
"Specify this option multiple times to enable the request for\n" __indent \
"more than one host.\n", \
.arg_description = "FILE", \
}
/* NOTE REQUIRED */
#define _entry_certs(__arg_data, __indent) \
{ \
.long_name = "cert", .short_name = 'C', .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME_ARRAY, .arg_data = __arg_data, \
.description = "FILE contains a certificate that is used to\n" __indent \
"establish a chain of trust for the verification\n" __indent \
"of the host-key documents. The IBM Z signing\n" __indent \
"key and intermediate CA certificate (signed\n" __indent \
"by the root CA) are required.\n", \
.arg_description = "FILE", \
}
/* NOTE REQUIRED */
#define _entry_crls(__arg_data, __indent) \
{ \
.long_name = "crl", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME_ARRAY, .arg_data = __arg_data, \
.description = "FILE contains a certificate revocation list (optional).\n", \
.arg_description = "FILE", \
}
/* NOTE REQUIRED */
#define _entry_root_ca(__arg_data, __indent) \
{ \
.long_name = "root-ca", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME_ARRAY, .arg_data = __arg_data, \
.description = "Use FILE as the trusted root CA instead the\n" __indent \
"root CAs that are installed on the system (optional).\n", \
.arg_description = "FILE", \
}
/* NOTE REQUIRED */
#define _entry_guest_hdr(__arg_data, __indent) \
{ \
.long_name = "hdr", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME, .arg_data = __arg_data, \
.description = "FILE specifies the header of the guest image.\n" __indent \
"Exactly one is required.\n", \
.arg_description = "FILE", \
}
/* NOTE REQUIRED */
#define _entry_input(__arg_data, __additional_text, __indent) \
{ \
.long_name = "input", .short_name = 'i', .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME, .arg_data = __arg_data, \
.description = "FILE specifies the " __additional_text " as input.\n", \
.arg_description = "FILE", \
}
/* NOTE REQUIRED */
#define _entry_output(__arg_data, __additional_text, __indent) \
{ \
.long_name = "output", .short_name = 'o', .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME, .arg_data = __arg_data, \
.description = "FILE specifies the output for the " __additional_text "\n", \
.arg_description = "FILE", \
}
/* NOTE REQUIRED */
#define _entry_att_prot_key_save(__arg_data, __indent) \
{ \
.long_name = "arpk", .short_name = 'a', .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME, .arg_data = __arg_data, \
.description = \
"Save the protection key as GCM-AES256 key in FILE\n" __indent \
"Do not publish this key, otherwise your attestation is compromised.\n", \
.arg_description = "FILE", \
}
/* NOTE REQUIRED */
#define _entry_att_prot_key_load(__arg_data, __indent) \
{ \
.long_name = "arpk", .short_name = 'a', .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME, .arg_data = __arg_data, \
.description = "Use FILE to specify the GCM-AES256 key to decrypt\n" __indent \
"the attestation request.\n" __indent \
"Delete this key after verification.\n", \
.arg_description = "FILE", \
}
#define _entry_phkh_img(__arg_data, __indent) \
{ \
.long_name = "x-phkh-img", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_NONE, .arg_data = __arg_data, \
.description = "Add the public host key hash of the\n" __indent \
"image header used to decrypt\n" __indent \
"the secure guest to the measurement. (optional)\n" \
}
#define _entry_phkh_att(__arg_data, __indent) \
{ \
.long_name = "x-phkh-att", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_NONE, .arg_data = __arg_data, \
.description = "Add the public host key hash of the\n" __indent \
"attestation header used to decrypt\n" __indent \
"the attestation request to the measurement. (optional)\n" \
}
#define _entry_no_verify(__arg_data, __indent) \
{ \
.long_name = "no-verify", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_NONE, .arg_data = __arg_data, \
.description = "Disable the host-key document verification.\n" __indent \
"(optional)\n", \
}
#define _entry_offline_maps_to_online(__arg_data, __indent) \
{ \
.long_name = "offline", .short_name = 0, .flags = G_OPTION_FLAG_REVERSE, \
.arg = G_OPTION_ARG_NONE, .arg_data = __arg_data, \
.description = "Don't download CRLs. (optional)\n", \
}
#define _entry_verbose(__indent) \
{ \
.long_name = "verbose", .short_name = 'V', .flags = G_OPTION_FLAG_NO_ARG, \
.arg = G_OPTION_ARG_CALLBACK, .arg_data = &increase_log_lvl, \
.description = "Provide more detailed output. (optional)\n", \
.arg_description = NULL, \
}
#define _entry_x_paf(__arg_data, __indent) \
{ \
.long_name = "x-paf", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_CALLBACK, .arg_data = __arg_data, \
.description = "Specify the Plain text Attestation Flags\n" __indent \
"as a hexadecimal value. Flags that change\n" __indent \
"the paf (--phkh-*) take precedence over\n" __indent \
"this flag.\n" __indent \
"Setting the nonce paf is not allowed here.\n" __indent \
"(optional, default 0x0)\n", \
.arg_description = "HEX", \
}
#define _entry_x_no_nonce(__arg_data, __indent) \
{ \
.long_name = "x-no-nonce", .short_name = 0, .flags = G_OPTION_FLAG_REVERSE, \
.arg = G_OPTION_ARG_NONE, .arg_data = __arg_data, \
.description = "Do not use a nonce in the request.\n" __indent \
"(optional, not recommended)\n" \
}
#define _entry_x_aad_size(__arg_data, __indent) \
{ \
.long_name = "x-add-size", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_INT, .arg_data = __arg_data, \
.description = "Specify the size of the additional area\n" __indent \
"Overwrite every flag that changes\n" __indent \
"this size implicitly. No verification is performed!\n" __indent \
"Ignored if negative.\n" __indent "(optional, default ignored)\n", \
.arg_description = "INT" \
}
#define _entry_x_user_data(__arg_data, __indent) \
{ \
.long_name = "x-user-data", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_FILENAME, .arg_data = __arg_data, \
.description = "Use FILE to specify the user data.\n", .arg_description = "FILE", \
}
#define _entry__verify_format(__indent) \
{ \
.long_name = "format", .short_name = 0, .flags = G_OPTION_FLAG_NONE, \
.arg = G_OPTION_ARG_CALLBACK, .arg_data = &set_verify_output_format, \
.description = "Define the output format.\n" __indent \
"Defaults to 'yaml'. (possible values: 'yaml')\n", \
.arg_description = "FORMAT", \
}
static gboolean increase_log_lvl(G_GNUC_UNUSED const char *option_name,
G_GNUC_UNUSED const char *value, G_GNUC_UNUSED void *data,
G_GNUC_UNUSED GError **error)
{
pvattest_log_increase_log_lvl(&pvattest_config.general.log_level);
return TRUE;
}
static gboolean set_verify_output_format(const char *option_name, const char *value,
G_GNUC_UNUSED void *data, GError **error)
{
if (!g_strcmp0(value, "yaml")) {
pvattest_config.verify.output_fmt = VERIFY_FMT_YAML;
} else {
g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
_("Found value '%s' for option '%s', but only 'yaml' is allowed."),
value, option_name);
return FALSE;
}
return TRUE;
}
static gboolean create_set_paf(G_GNUC_UNUSED const char *option_name, const char *value,
G_GNUC_UNUSED void *data, GError **error)
{
return hex_str_toull(value, &pvattest_config.create.paf, error);
}
/***************************** GENERAL OPTIONS ********************************/
static gboolean print_version = FALSE;
static GOptionEntry general_options[] = {
{
.long_name = "version",
.short_name = 'v',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_NONE,
.arg_data = &print_version,
.description = "Print the version and exit.\n",
.arg_description = NULL,
},
_entry_verbose(""),
{ NULL },
};
/************************* CREATE ATTESTATION OPTIONS *************************/
#define create_indent " "
static GOptionEntry create_options[] = {
_entry_host_key_document(&pvattest_config.create.host_key_document_paths, create_indent),
_entry_certs(&pvattest_config.create.certificate_paths, create_indent),
_entry_crls(&pvattest_config.create.crl_paths, create_indent),
_entry_root_ca(&pvattest_config.create.root_ca_path, create_indent),
_entry_output(&pvattest_config.create.output_path, "attestation request", create_indent),
_entry_att_prot_key_save(&pvattest_config.create.arp_key_out_path, create_indent),
_entry_no_verify(&pvattest_config.create.no_verify, create_indent),
_entry_offline_maps_to_online(&pvattest_config.create.online, create_indent),
_entry_verbose(create_indent),
{ NULL }
};
static GOptionEntry experimental_create_options[] = {
_entry_x_no_nonce(&pvattest_config.create.use_nonce, create_indent),
_entry_x_paf(&create_set_paf, create_indent),
_entry_x_aad_size(&pvattest_config.create.x_aad_size, create_indent),
_entry_phkh_img(&pvattest_config.create.phkh_img, create_indent),
_entry_phkh_att(&pvattest_config.create.phkh_att, create_indent),
{ NULL }
};
static gboolean verify_create(GError **error)
{
if (!check_file_list(pvattest_config.create.host_key_document_paths, TRUE,
_("Specify --host-key-document at least once."), error))
return FALSE;
if (!pvattest_config.create.no_verify) {
if (!check_file_list(
pvattest_config.create.certificate_paths, TRUE,
_("Either specify the IBM Z signing key and"
" intermediate CA certificate\nby using the '--cert' option, or"
" use the '--no-verify' flag to disable the\nhost-key document"
" verification completely (at your own risk).\n"
"Only use this option in test environments or if"
" you trust the unverified document."),
error))
return FALSE;
}
if (!check_for_invalid_path(pvattest_config.create.arp_key_out_path, FALSE,
_("Missing argument for --arpk."), error))
return FALSE;
if (!check_for_invalid_path(pvattest_config.create.output_path, FALSE,
_("Missing argument for --output."), error))
return FALSE;
if (!check_optional_file_list(pvattest_config.create.crl_paths, TRUE, error))
return FALSE;
if (!check_for_optional_invalid_path(pvattest_config.create.root_ca_path, TRUE, error))
return FALSE;
return TRUE;
};
/************************* MEASUREMENT OPTIONS ********************************/
#define perform_indent " "
static GOptionEntry perform_options[] = {
_entry_input(&pvattest_config.perform.input_path, "attestation request", perform_indent),
_entry_output(&pvattest_config.perform.output_path, "attestation result", perform_indent),
_entry_verbose(perform_indent),
{ NULL },
};
static GOptionEntry experimental_perform_options[] = {
_entry_x_user_data(&pvattest_config.perform.user_data_path, perform_indent),
{ NULL },
};
static gboolean verify_perform(GError **error)
{
if (!check_for_invalid_path(pvattest_config.perform.input_path, TRUE,
_("Missing argument for --input."), error))
return FALSE;
if (!check_for_invalid_path(pvattest_config.perform.output_path, FALSE,
_("Missing argument for --output."), error))
return FALSE;
if (!check_for_optional_invalid_path(pvattest_config.perform.user_data_path, TRUE, error))
return FALSE;
return TRUE;
}
/************************* VERIFY OPTIONS ************************************/
#define verify_indent " "
static GOptionEntry verify_options[] = {
_entry_input(&pvattest_config.verify.input_path, "attestation result", verify_indent),
_entry_output(&pvattest_config.verify.output_path,
"verification result.\n" verify_indent "(optional)", verify_indent),
_entry_guest_hdr(&pvattest_config.verify.hdr_path, verify_indent),
_entry_att_prot_key_load(&pvattest_config.verify.arp_key_in_path, verify_indent),
_entry_verbose(verify_indent),
_entry__verify_format(verify_indent),
{ NULL },
};
static gboolean verify_verify(GError **error)
{
if (!check_for_invalid_path(pvattest_config.verify.input_path, TRUE,
_("Missing argument for --input."), error))
return FALSE;
if (!check_for_invalid_path(pvattest_config.verify.hdr_path, TRUE,
_("Missing argument for --hdr."), error))
return FALSE;
if (!check_for_invalid_path(pvattest_config.verify.arp_key_in_path, TRUE,
_("Missing argument for --arpk."), error))
return FALSE;
return TRUE;
}
/************************** OPTIONS END ***************************************/
#pragma GCC diagnostic pop
static char summary[] =
"\n"
"Create, perform, and verify attestation measurements for IBM Secure Execution guest"
" systems.\n"
"\n"
"COMMANDS\n"
" create On a trusted system, creates an attestation request.\n"
" perform On the SE-guest to be attested, sends the attestation request\n"
" to the Ultravisor and receives the answer.\n"
#ifndef PVATTEST_COMPILE_PERFORM
" (not supported on this platform)\n"
#endif /* PVATTEST_COMPILE_PERFORM */
" verify On a trusted system, compares the one from your trusted system.\n"
" If they differ, the Secure Execution guest might not be compromised\n"
"\n"
"Use '" GETTEXT_PACKAGE " [COMMAND] -h' to get detailed help\n";
static char create_summary[] =
"Create attestation measurement requests to attest an\n"
"IBM Secure Execution guest. Only build attestation requests in a trusted\n"
"environment such as your Workstation.\n"
"To avoid compromising the attestation do not publish the\n"
"protection key and delete it after verification.\n"
"Every 'create' will generate a new, random protection key.\n";
static char perform_summary[] =
#ifndef PVATTEST_COMPILE_PERFORM
"This system does NOT support 'perform'.\n"
#endif /* PVATTEST_COMPILE_PERFORM */
"Perform a measurement of this IBM Secure Execution guest using '/dev/uv'.\n";
static char verify_summary[] =
"Verify that a previously generated attestation measurement of an\n"
"IBM Secure Execution guest yielded the expected results.\n"
"Verify attestation requests only in a trusted environment, such as your workstation.";
static void print_version_and_exit(void)
{
printf("%s version %s\n", GETTEXT_PACKAGE, RELEASE_STRING);
printf("%s\n", COPYRIGHT_NOTICE);
exit(EXIT_SUCCESS);
}
static GOptionContext *create_ctx(GOptionEntry *options, GOptionEntry *experimental_options,
const char *param_name, const char *opt_summary)
{
GOptionContext *ret = g_option_context_new(param_name);
GOptionGroup *x_group = NULL;
g_option_context_add_main_entries(ret, options, NULL);
g_option_context_set_summary(ret, opt_summary);
if (experimental_options) {
x_group = g_option_group_new(
"experimental",
"Experimental Options; Do not use in a production environment",
"Show experimental options", NULL, NULL);
g_option_group_add_entries(x_group, experimental_options);
g_option_context_add_group(ret, x_group);
}
return ret;
}
enum pvattest_command pvattest_parse(int *argc, char **argvp[], pvattest_config_t **config,
GError **error)
{
g_autoptr(GOptionContext) main_context = NULL, subc_context = NULL;
char **argv = *argvp;
enum pvattest_command subc = PVATTEST_SUBC_INVALID;
verify_options_fn_t verify_options_fn = NULL;
pv_wrapped_g_assert(argc);
pv_wrapped_g_assert(argvp);
pv_wrapped_g_assert(config);
/*
* First parse until the first non dash argument. This must be one of the commands.
* (strict POSIX parsing)
*/
main_context = g_option_context_new(
"COMMAND [OPTIONS] - create, perform, and verify attestation measurements");
g_option_context_set_strict_posix(main_context, TRUE);
g_option_context_add_main_entries(main_context, general_options, NULL);
g_option_context_set_summary(main_context, summary);
if (!g_option_context_parse(main_context, argc, argvp, error))
return PVATTEST_SUBC_INVALID;
if (print_version)
print_version_and_exit();
/*
* Parse depending on the specified command
*/
else if (g_strcmp0(argv[1], PVATTEST_SUBC_STR_CREATE) == 0) {
subc_context =
create_ctx(create_options, experimental_create_options,
"create [OPTIONS] - create an attestation measurement request",
create_summary);
subc = PVATTEST_SUBC_CREATE;
verify_options_fn = &verify_create;
} else if (g_strcmp0(argv[1], PVATTEST_SUBC_STR_PERFORM) == 0) {
subc_context =
create_ctx(perform_options, experimental_perform_options,
"perform [OPTIONS] - perform an attestation measurement request",
perform_summary);
subc = PVATTEST_SUBC_PERFORM;
verify_options_fn = &verify_perform;
#ifndef PVATTEST_COMPILE_PERFORM
g_set_error(error, PVATTEST_ERROR, PVATTEST_ERR_INV_ARG,
_("This system does not support the 'perform' command."));
return PVATTEST_SUBC_INVALID;
#endif /* PVATTEST_COMPILE_PERFORM */
} else if (g_strcmp0(argv[1], PVATTEST_SUBC_STR_VERIFY) == 0) {
subc_context = create_ctx(verify_options, NULL,
"verify [OPTIONS] - verify an attestation measurement",
verify_summary);
subc = PVATTEST_SUBC_VERIFY;
verify_options_fn = &verify_verify;
} else {
if (argv[1])
g_set_error(error, PVATTEST_ERROR, PVATTEST_ERR_INV_ARGV,
_("Invalid command specified: %s."), argv[1]);
else
g_set_error(error, PVATTEST_ERROR, PVATTEST_ERR_INV_ARGV,
_("No command specified."));
return PVATTEST_SUBC_INVALID;
}
g_assert(verify_options_fn);
if (!g_option_context_parse(subc_context, argc, argvp, error))
return PVATTEST_SUBC_INVALID;
if (!verify_options_fn(error))
return PVATTEST_SUBC_INVALID;
*config = &pvattest_config;
return subc;
}
static void pvattest_parse_clear_create_config(pvattest_create_config_t *config)
{
if (!config)
return;
g_strfreev(config->host_key_document_paths);
g_strfreev(config->certificate_paths);
g_free(config->arp_key_out_path);
g_free(config->output_path);
}
static void pvattest_parse_clear_perform_config(pvattest_perform_config_t *config)
{
if (!config)
return;
g_free(config->input_path);
g_free(config->output_path);
}
static void pvattest_parse_clear_verify_config(pvattest_verify_config_t *config)
{
if (!config)
return;
g_free(config->input_path);
g_free(config->output_path);
g_free(config->hdr_path);
g_free(config->arp_key_in_path);
}
void pvattest_parse_clear_config(pvattest_config_t *config)
{
if (!config)
return;
pvattest_parse_clear_create_config(&config->create);
pvattest_parse_clear_perform_config(&config->perform);
pvattest_parse_clear_verify_config(&config->verify);
}

View File

@@ -1,113 +0,0 @@
/*
* Definitions used for parsing arguments.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef PVATTEST_ARGPARSE_H
#define PVATTEST_ARGPARSE_H
/* Must be included before any other header */
#include "config.h"
#include <stdint.h>
#include "libpv/glib-helper.h"
#include "libpv/macros.h"
#define PVATTEST_SUBC_STR_CREATE "create"
#define PVATTEST_SUBC_STR_PERFORM "perform"
#define PVATTEST_SUBC_STR_VERIFY "verify"
enum pvattest_command {
PVATTEST_SUBC_INVALID,
PVATTEST_SUBC_CREATE,
PVATTEST_SUBC_PERFORM,
PVATTEST_SUBC_VERIFY,
};
typedef struct {
int log_level;
} pvattest_general_config_t;
typedef struct {
char **host_key_document_paths;
char **certificate_paths;
char **crl_paths;
char *root_ca_path;
char *arp_key_out_path;
char *output_path;
gboolean phkh_img;
gboolean phkh_att;
gboolean no_verify;
gboolean online;
/* experimental flags */
gboolean use_nonce; /* default TRUE */
uint64_t paf; /* default 0 */
int x_aad_size; /* default -1 -> ignore */
} pvattest_create_config_t;
typedef struct {
char *output_path;
char *input_path;
/* experimental flags */
char *user_data_path; /* default NULL */
} pvattest_perform_config_t;
enum verify_output_format {
VERIFY_FMT_HUMAN,
VERIFY_FMT_YAML,
};
typedef struct {
char *input_path;
char *output_path;
enum verify_output_format output_fmt;
char *hdr_path;
char *arp_key_in_path;
} pvattest_verify_config_t;
typedef struct {
pvattest_general_config_t general;
pvattest_create_config_t create;
pvattest_perform_config_t perform;
pvattest_verify_config_t verify;
} pvattest_config_t;
/**
* pvattest_parse_clear_config:
*
* @config: struct to be cleared
*
* clears but not frees all config.
* all non config members such like char* will be freed.
*/
void pvattest_parse_clear_config(pvattest_config_t *config);
/**
* pvattest_parse:
*
* @argc: ptr to argument count
* @argv: ptr to argument vector
* @config: output: ptr to parsed config. Target is statically allocated.
* You are responsible for freeing all non config ptrs.
* use #pvattest_parse_clear_config for that.
*
* Will not return if verbose or help parsed.
*
* Returns: selected command as enum
*/
enum pvattest_command pvattest_parse(int *argc, char **argvp[], pvattest_config_t **config,
GError **error) PV_NONNULL(1, 2, 3);
#define PVATTEST_ERROR g_quark_from_static_string("pv-pvattest_error-quark")
typedef enum {
PVATTEST_ERR_INV_ARGV,
PVATTEST_ERR_INV_ARG,
} pv_pvattest_error_e;
#endif /* PVATTEST_ARGPARSE_H */

View File

@@ -1,148 +0,0 @@
/*
* Attestation related functions
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include "libpv/cert.h"
#include "libpv/hash.h"
#include "libpv/se-hdr.h"
#include "exchange_format.h"
#include "attestation.h"
G_STATIC_ASSERT(sizeof(((att_meas_ctx_t *)0)->pld) == sizeof(((struct pv_hdr_head *)0)->pld));
G_STATIC_ASSERT(sizeof(((att_meas_ctx_t *)0)->ald) == sizeof(((struct pv_hdr_head *)0)->ald));
G_STATIC_ASSERT(sizeof(((att_meas_ctx_t *)0)->tld) == sizeof(((struct pv_hdr_head *)0)->tld));
G_STATIC_ASSERT(sizeof(((att_meas_ctx_t *)0)->tag) == sizeof(((struct pv_hdr *)0)->tag));
struct att_meas_sizes {
uint16_t user_data_len;
uint16_t zeros;
uint32_t additional_data_len;
} __packed;
G_STATIC_ASSERT(sizeof(struct att_meas_sizes) == 8);
/*
* All optional arguments may be NULL
* user_data is up to 256 bytes long, or NULL.
* nonce is 16 bytes long or NULL.
* additional_data is up to 32768 bytes long or NULL.
*/
GBytes *att_gen_measurement_hmac_sha512(const att_meas_ctx_t *meas_ctx, GBytes *measurement_key,
GBytes *optional_user_data, GBytes *optional_nonce,
GBytes *optional_additional_data, GError **error)
{
struct att_meas_sizes meas_sizes = {};
g_autoptr(HMAC_CTX) hmac_ctx = NULL;
size_t additional_data_size = 0;
size_t user_data_size = 0;
size_t nonce_size = 0;
pv_wrapped_g_assert(meas_ctx);
pv_wrapped_g_assert(measurement_key);
if (optional_user_data)
user_data_size = g_bytes_get_size(optional_user_data);
if (optional_additional_data)
additional_data_size = g_bytes_get_size(optional_additional_data);
if (optional_nonce)
nonce_size = g_bytes_get_size(optional_nonce);
/* checks for these sizes resulting in GErrors are done before */
g_assert(user_data_size <= PVATTEST_USER_DATA_MAX_SIZE);
g_assert(additional_data_size <= PVATTEST_ADDITIONAL_MAX_SIZE);
g_assert(nonce_size == 0 || nonce_size == ARCB_V1_NONCE_SIZE);
pv_wrapped_g_assert(meas_ctx);
pv_wrapped_g_assert(measurement_key);
hmac_ctx = pv_hmac_ctx_new(measurement_key, EVP_sha512(), error);
if (!hmac_ctx)
return NULL;
meas_sizes.user_data_len = GUINT16_TO_BE((uint16_t)user_data_size);
meas_sizes.zeros = 0;
meas_sizes.additional_data_len = GUINT32_TO_BE((uint32_t)additional_data_size);
if (pv_hmac_ctx_update_raw(hmac_ctx, meas_ctx, sizeof(*meas_ctx), error) != 0)
return NULL;
/* add the sizes of user and additional data. */
if (pv_hmac_ctx_update_raw(hmac_ctx, &meas_sizes, sizeof(meas_sizes), error))
return NULL;
/* update optional data. if NULL passed (or size = 0) nothing will happen to the HMAC_CTX */
if (pv_hmac_ctx_update(hmac_ctx, optional_user_data, error) != 0)
return NULL;
if (pv_hmac_ctx_update(hmac_ctx, optional_nonce, error) != 0)
return NULL;
if (pv_hmac_ctx_update(hmac_ctx, optional_additional_data, error) != 0)
return NULL;
return pv_hamc_ctx_finalize(hmac_ctx, error);
}
att_meas_ctx_t *att_extract_from_hdr(GBytes *se_hdr, GError **error)
{
g_autofree att_meas_ctx_t *meas = NULL;
const struct pv_hdr *hdr = NULL;
size_t se_hdr_tag_offset;
size_t se_hdr_size;
uint8_t *hdr_u8;
pv_wrapped_g_assert(se_hdr);
hdr = g_bytes_get_data(se_hdr, &se_hdr_size);
hdr_u8 = (uint8_t *)hdr;
if (se_hdr_size < PV_V1_PV_HDR_MIN_SIZE) {
g_set_error(error, ATT_ERROR, ATT_ERR_INVALID_HDR,
_("Invalid SE header provided."));
return NULL;
}
if (GUINT32_FROM_BE(hdr->head.phs) != se_hdr_size ||
GUINT64_FROM_BE(hdr->head.magic) != PV_MAGIC_NUMBER) {
g_set_error(error, ATT_ERROR, ATT_ERR_INVALID_HDR,
_("Invalid SE header provided."));
return NULL;
}
se_hdr_tag_offset = GUINT32_FROM_BE(hdr->head.phs) - sizeof(hdr->tag);
meas = g_new0(att_meas_ctx_t, 1);
memcpy(meas->pld, hdr->head.pld, sizeof(meas->pld));
memcpy(meas->ald, hdr->head.ald, sizeof(meas->ald));
memcpy(meas->tld, hdr->head.tld, sizeof(meas->tld));
memcpy(meas->tag, hdr_u8 + se_hdr_tag_offset, sizeof(meas->tag));
return g_steal_pointer(&meas);
}
void att_add_uid(att_meas_ctx_t *meas_ctx, GBytes *config_uid)
{
pv_wrapped_g_assert(meas_ctx);
pv_wrapped_g_assert(config_uid);
g_assert(g_bytes_get_size(config_uid) == ATT_CONFIG_UID_SIZE);
pv_gbytes_memcpy(meas_ctx->config_uid, ATT_CONFIG_UID_SIZE, config_uid, NULL);
}
gboolean att_verify_measurement(const GBytes *calculated_measurement,
const GBytes *uvio_measurement, GError **error)
{
pv_wrapped_g_assert(calculated_measurement);
pv_wrapped_g_assert(uvio_measurement);
if (g_bytes_compare(calculated_measurement, uvio_measurement) != 0) {
g_set_error(error, ATT_ERROR, ATT_ERR_MEASUREMENT_VERIFICATION_FAILED,
_("Calculated and received attestation measurement are not equal."));
return FALSE;
}
return TRUE;
}

View File

@@ -1,99 +0,0 @@
/*
* Attestation related functions and definitions.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef PVATTEST_ATTESTATION_H
#define PVATTEST_ATTESTATION_H
/* Must be included before any other header */
#include "config.h"
#include <openssl/sha.h>
#include "libpv/glib-helper.h"
#include "libpv/crypto.h"
#include "libpv/cert.h"
#include "common.h"
#include "types.h"
#include "arcb.h"
#define ATT_CONFIG_UID_SIZE 16
typedef struct {
uint8_t pld[SHA512_DIGEST_LENGTH];
uint8_t ald[SHA512_DIGEST_LENGTH];
uint8_t tld[SHA512_DIGEST_LENGTH];
uint8_t tag[AES_256_GCM_TAG_SIZE];
uint8_t config_uid[ATT_CONFIG_UID_SIZE];
} __packed att_meas_ctx_t;
G_STATIC_ASSERT(sizeof(att_meas_ctx_t) == 224);
/**
* att_gen_measurement_hmac_sha512:
*
* @meas_ctx: measurement context.
* @measurement_key: AES-256-GCM key for generating the measurement calculation.
* @optional_user_data: NULL or up to 256 bytes GBytes.
* @optional_nonce: NULL or a nonce of exactly `ARCB_V1_NONCE_SIZE` bytes
* @optional_additional_data: NULL or up to 0x8000 bytes of GBytes.
* @error: GError. *error will != NULL if error occurs.
*
* Calculates the measurement value.
* If the input data is the same which the UV used in the Retrieve Attestation Measurement
* the result should be identical to the data in the ´Measurement Data Address´ UVC.
*
* Returns: (nullable) (transfer full): a hmac_sha512 of the given data.
*/
GBytes *att_gen_measurement_hmac_sha512(const att_meas_ctx_t *meas_ctx, GBytes *measurement_key,
GBytes *optional_user_data, GBytes *optional_nonce,
GBytes *optional_additional_data, GError **error)
PV_NONNULL(1, 2);
/**
* att_extract_from_hdr:
*
* @se_hdr: binary SE guest header.
* @error: GError. *error will != NULL if error occurs.
*
* Verifies that SE header size and magic ,but no cryptographical verification.
* Then, find and extracts pld, ald, tld, and SE tagi and adds it to the context.
*
* Returns: new attestation measurement context.
*/
att_meas_ctx_t *att_extract_from_hdr(GBytes *se_hdr, GError **error) PV_NONNULL(1);
/** att_add_uid:
*
* @meas_ctx: measurement context.
* @config_uid: pointer to config UID. Must be `ATT_CONFIG_UID_SIZE` bytes long.
*
* Copies the config UID to the measurement context.
* Wrong size is considered as a Programming error.
*/
void att_add_uid(att_meas_ctx_t *meas_ctx, GBytes *config_uid) PV_NONNULL(1, 2);
/** att_verify_measurement:
*
* @calculated_measurement: measurement calculated by a trusted system
* @uvio_measurement: measurement generated by an UV
* @error: GError. *error will != NULL if error occurs.
*
* Returns: TRUE if measurements are identical, otherwise FALSE
*/
gboolean att_verify_measurement(const GBytes *calculated_measurement,
const GBytes *uvio_measurement, GError **error) PV_NONNULL(1, 2);
#define ATT_ERROR g_quark_from_static_string("pv-att_error-quark")
typedef enum att_error {
ATT_ERR_INVALID_HDR,
ATT_ERR_INVALID_USER_DATA,
ATT_ERR_MEASUREMENT_VERIFICATION_FAILED,
ATT_ERR_PHKH_NO_FIT_IN_USER_DATA,
ATT_ERR_PHKH_NO_MATCH,
} att_error_e;
#endif /* PVATTEST_ATTESTATION_H */

View File

@@ -1,47 +0,0 @@
/*
* Common functions for pvattest.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include <stdio.h>
#include <sys/stat.h>
#include "libpv/glib-helper.h"
#include "types.h"
#include "common.h"
gboolean wrapped_g_file_set_content(const char *filename, GBytes *bytes, mode_t mode,
GError **error)
{
const void *data;
size_t size;
gboolean rc;
data = g_bytes_get_data(bytes, &size);
rc = g_file_set_contents(filename, data, (ssize_t)size, error);
if (rc && mode != 0666)
chmod(filename, mode);
return rc;
}
GBytes *secure_gbytes_concat(GBytes *lh, GBytes *rh)
{
g_autoptr(GByteArray) lha = NULL;
if (!lh && !rh)
return NULL;
if (!lh)
return g_bytes_ref(rh);
if (!rh)
return g_bytes_ref(lh);
lha = g_bytes_unref_to_array(g_bytes_ref(lh));
g_byte_array_append(lha, g_bytes_get_data(rh, NULL), (guint)g_bytes_get_size(rh));
return pv_sec_gbytes_new(lha->data, lha->len);
}

View File

@@ -1,37 +0,0 @@
/*
* Common functions for pvattest.
*
* IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef PVATTEST_COMMON_H
#define PVATTEST_COMMON_H
/* Must be included before any other header */
#include "config.h"
#include <stdio.h>
#include "libpv/glib-helper.h"
#include <glib/gi18n.h>
#include "libpv/macros.h"
#include "lib/zt_common.h"
#include "types.h"
#define COPYRIGHT_NOTICE "Copyright IBM Corp. 2022"
#define AES_256_GCM_TAG_SIZE 16
gboolean wrapped_g_file_set_content(const char *filename, GBytes *bytes, mode_t mode,
GError **error);
/**
* just ref's up if one of them is NULL.
* If both NULL returns NULL.
* Otherwise returns lh ++ rh
*/
GBytes *secure_gbytes_concat(GBytes *lh, GBytes *rh);
#endif /* PVATTEST_COMMON_H */

View File

@@ -1,31 +0,0 @@
/*
* Config file.
* Must be include before any other header.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*
*/
#ifndef PVATTEST_CONFIG_H
#define PVATTEST_CONFIG_H
#define GETTEXT_PACKAGE "pvattest"
#ifdef __GNUC__
#ifdef __s390x__
#ifndef PVATTEST_NO_PERFORM
#define PVATTEST_COMPILE_PERFORM
#endif
#endif
#endif
#ifdef __clang__
#ifdef __zarch__
#ifndef PVATTEST_NO_PERFORM
#define PVATTEST_COMPILE_PERFORM
#endif
#endif
#endif
#endif /* PVATTEST_CONFIG_H */

View File

@@ -1,479 +0,0 @@
/*
* Functions for the pvattest exchange format to send attestation requests and responses between
* machines .
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "common.h"
#include "exchange_format.h"
#include "log.h"
struct exchange_shared_hdr {
be64_t magic;
be32_t version;
be32_t size;
} __packed;
/*
* If size == 0
* offset ignored.
* (part does not exist)
* if offset >0 and <0x50 -> invalid format
* if offset == 0 and size > 0 no data saved, however the request will need this amount of memory to
* succeed.
* Only makes sense for measurement and additional data. This however, is not enforced.
*/
struct entry {
be32_t size;
be32_t offset;
} __packed;
G_STATIC_ASSERT(sizeof(struct entry) == 8);
struct _exchange_format_v1_hdr {
be64_t magic;
be32_t version;
be32_t size;
uint64_t reserved;
struct entry serialized_arcb;
struct entry measurement;
struct entry additional_data;
struct entry user_data;
struct entry config_uid;
} __packed;
G_STATIC_ASSERT(sizeof(exchange_format_v1_hdr_t) == 0x40);
struct _exchange_format_ctx {
uint32_t version;
uint32_t req_meas_size;
uint32_t req_add_size;
GBytes *serialized_arcb;
GBytes *measurement;
GBytes *additional_data;
GBytes *user_data;
GBytes *config_uid;
};
/* Use a byte array to avoid any byteorder issues while checking */
#define PVATTEST_EXCHANGE_MAGIC 0x7076617474657374 /* pvattest */
static const uint8_t exchange_magic[] = { 0x70, 0x76, 0x61, 0x74, 0x74, 0x65, 0x73, 0x74 };
exchange_format_ctx_t *exchange_ctx_new(uint32_t version, GBytes *serialized_arcb,
uint32_t req_measurement_size, uint32_t req_additional_size,
GError **error)
{
g_autoptr(exchange_format_ctx_t) ctx = NULL;
pv_wrapped_g_assert(serialized_arcb);
if (version != PVATTEST_EXCHANGE_VERSION_1_00) {
g_set_error(error, EXCHANGE_FORMAT_ERROR, EXCHANGE_FORMAT_ERROR_UNSUPPORTED_VERSION,
_("'%d' unsupported version."), version);
return NULL;
}
ctx = g_malloc0(sizeof(*ctx));
ctx->version = version;
exchange_set_serialized_arcb(ctx, serialized_arcb);
ctx->req_meas_size = req_measurement_size;
ctx->req_add_size = req_additional_size;
return g_steal_pointer(&ctx);
}
static GBytes *get_content(GBytes *file_content, const struct entry *entry, const size_t max_size,
GError **error)
{
uint64_t size = GUINT32_FROM_BE(entry->size);
uint64_t offset = GUINT32_FROM_BE(entry->offset);
size_t file_size = 0;
const uint8_t *file_content_u8 = g_bytes_get_data(file_content, &file_size);
if (size == 0 || offset == 0)
return NULL;
if (offset < sizeof(exchange_format_v1_hdr_t) || offset + size > file_size ||
size > max_size) {
g_set_error(error, EXCHANGE_FORMAT_ERROR, EXCHANGE_FORMAT_ERROR_INVALID_FORMAT,
_("Input file is not in a valid format."));
return NULL;
}
return g_bytes_new(file_content_u8 + offset, size);
}
static gboolean check_format(const struct exchange_shared_hdr *hdr)
{
if (memcmp(exchange_magic, &hdr->magic, sizeof(exchange_magic)) == 0)
return TRUE;
return FALSE;
}
exchange_format_ctx_t *exchange_ctx_from_file(const char *filename, GError **error)
{
g_autoptr(exchange_format_ctx_t) ctx = g_malloc0(sizeof(*ctx));
const struct exchange_shared_hdr *hdr = NULL;
const exchange_format_v1_hdr_t *hdr_v1 = NULL;
g_autoptr(GBytes) file_content = NULL;
size_t config_uid_size = 0;
size_t file_size;
pv_wrapped_g_assert(filename);
file_content = pv_file_get_content_as_g_bytes(filename, error);
if (!file_content)
return NULL;
hdr = (const struct exchange_shared_hdr *)g_bytes_get_data(file_content, &file_size);
if (file_size < sizeof(*hdr) || !check_format(hdr)) {
g_set_error(error, EXCHANGE_FORMAT_ERROR, EXCHANGE_FORMAT_ERROR_INVALID_FORMAT,
_("'%s' is not in a valid format."), filename);
return NULL;
}
if (GUINT32_FROM_BE(hdr->version) != PVATTEST_EXCHANGE_VERSION_1_00) {
g_set_error(error, EXCHANGE_FORMAT_ERROR, EXCHANGE_FORMAT_ERROR_INVALID_FORMAT,
_("The version (%#x) of '%s' is not supported"),
GUINT32_FROM_BE(hdr->version), filename);
return NULL;
}
/* get the header */
if (file_size < sizeof(exchange_format_v1_hdr_t)) {
g_set_error(error, EXCHANGE_FORMAT_ERROR, EXCHANGE_FORMAT_ERROR_INVALID_FORMAT,
_("'%s' is not in a valid format."), filename);
return NULL;
}
hdr_v1 = (const exchange_format_v1_hdr_t *)hdr;
/* get entries if present */
ctx->serialized_arcb =
get_content(file_content, &hdr_v1->serialized_arcb, PVATTEST_ARCB_MAX_SIZE, error);
if (*error)
return NULL;
ctx->measurement = get_content(file_content, &hdr_v1->measurement,
PVATTEST_MEASUREMENT_MAX_SIZE, error);
if (*error)
return NULL;
ctx->additional_data = get_content(file_content, &hdr_v1->additional_data,
PVATTEST_ADDITIONAL_MAX_SIZE, error);
if (*error)
return NULL;
ctx->user_data =
get_content(file_content, &hdr_v1->user_data, PVATTEST_USER_DATA_MAX_SIZE, error);
if (*error)
return NULL;
ctx->config_uid = get_content(file_content, &hdr_v1->config_uid, PVATTEST_UID_SIZE, error);
if (*error)
return NULL;
if (ctx->config_uid)
config_uid_size = g_bytes_get_size(ctx->config_uid);
if (config_uid_size != PVATTEST_UID_SIZE && config_uid_size != 0) {
g_set_error(error, EXCHANGE_FORMAT_ERROR, EXCHANGE_FORMAT_ERROR_INVALID_FORMAT,
_("'%s' is not in a valid format."), filename);
return NULL;
}
ctx->req_meas_size = GUINT32_FROM_BE(hdr_v1->measurement.size);
ctx->req_add_size = GUINT32_FROM_BE(hdr_v1->additional_data.size);
ctx->version = GUINT32_FROM_BE(hdr->version);
return g_steal_pointer(&ctx);
}
void clear_free_exchange_ctx(exchange_format_ctx_t *ctx)
{
if (!ctx)
return;
if (ctx->serialized_arcb)
g_bytes_unref(ctx->serialized_arcb);
if (ctx->measurement)
g_bytes_unref(ctx->measurement);
if (ctx->additional_data)
g_bytes_unref(ctx->additional_data);
if (ctx->user_data)
g_bytes_unref(ctx->user_data);
if (ctx->config_uid)
g_bytes_unref(ctx->config_uid);
g_free(ctx);
}
void exchange_set_serialized_arcb(exchange_format_ctx_t *ctx, GBytes *serialized_arcb)
{
pv_wrapped_g_assert(ctx);
pv_wrapped_g_assert(serialized_arcb);
g_bytes_ref(serialized_arcb);
g_bytes_unref(ctx->serialized_arcb);
ctx->serialized_arcb = serialized_arcb;
}
void exchange_set_measurement(exchange_format_ctx_t *ctx, GBytes *measurement)
{
pv_wrapped_g_assert(ctx);
pv_wrapped_g_assert(measurement);
g_bytes_ref(measurement);
g_bytes_unref(ctx->measurement);
ctx->measurement = measurement;
}
void exchange_set_additional_data(exchange_format_ctx_t *ctx, GBytes *additional_data)
{
pv_wrapped_g_assert(ctx);
pv_wrapped_g_assert(additional_data);
g_bytes_ref(additional_data);
g_bytes_unref(ctx->additional_data);
ctx->additional_data = additional_data;
}
void exchange_set_user_data(exchange_format_ctx_t *ctx, GBytes *user_data)
{
pv_wrapped_g_assert(ctx);
pv_wrapped_g_assert(user_data);
g_bytes_ref(user_data);
g_bytes_unref(ctx->user_data);
ctx->user_data = user_data;
}
void exchange_set_config_uid(exchange_format_ctx_t *ctx, GBytes *config_uid)
{
pv_wrapped_g_assert(ctx);
pv_wrapped_g_assert(config_uid);
g_bytes_ref(config_uid);
g_bytes_unref(ctx->config_uid);
ctx->config_uid = config_uid;
}
static GBytes *gbytes_ref0(GBytes *bytes)
{
if (!bytes)
return NULL;
return g_bytes_ref(bytes);
}
GBytes *exchange_get_serialized_arcb(const exchange_format_ctx_t *ctx)
{
pv_wrapped_g_assert(ctx);
return gbytes_ref0(ctx->serialized_arcb);
}
GBytes *exchange_get_measurement(const exchange_format_ctx_t *ctx)
{
pv_wrapped_g_assert(ctx);
return gbytes_ref0(ctx->measurement);
}
GBytes *exchange_get_additional_data(const exchange_format_ctx_t *ctx)
{
pv_wrapped_g_assert(ctx);
return gbytes_ref0(ctx->additional_data);
}
GBytes *exchange_get_user_data(const exchange_format_ctx_t *ctx)
{
pv_wrapped_g_assert(ctx);
return gbytes_ref0(ctx->user_data);
}
GBytes *exchange_get_config_uid(const exchange_format_ctx_t *ctx)
{
pv_wrapped_g_assert(ctx);
return gbytes_ref0(ctx->config_uid);
}
uint32_t exchange_get_requested_measurement_size(const exchange_format_ctx_t *ctx)
{
pv_wrapped_g_assert(ctx);
return ctx->req_meas_size;
}
uint32_t exchange_get_requested_additional_data_size(const exchange_format_ctx_t *ctx)
{
pv_wrapped_g_assert(ctx);
return ctx->req_add_size;
}
static struct entry add_g_bytes(GBytes *bytes, FILE *file, GError **error)
{
struct entry result = {};
long offset;
size_t size;
const void *data = g_bytes_get_data(bytes, &size);
g_assert(size <= G_MAXUINT32);
offset = pv_file_tell(file, error);
g_assert(offset <= G_MAXUINT32);
if (offset < 0)
return result;
result.offset = GUINT32_TO_BE((uint32_t)offset);
result.size = GUINT32_TO_BE((uint32_t)size);
pv_file_write(file, data, size, error);
return result;
}
int exchange_write_to_file(const exchange_format_ctx_t *ctx, const char *filename, GError **error)
{
exchange_format_v1_hdr_t hdr = {
.magic = GUINT64_TO_BE(PVATTEST_EXCHANGE_MAGIC),
.version = GUINT32_TO_BE(ctx->version),
};
size_t file_size = sizeof(hdr);
g_autoptr(FILE) file = NULL;
struct stat file_stat;
long actual_file_size;
size_t tmp_size;
pv_wrapped_g_assert(ctx);
pv_wrapped_g_assert(filename);
file = pv_file_open(filename, "w", error);
if (!file)
return -1;
if (fstat(fileno(file), &file_stat) != 0 || !S_ISREG(file_stat.st_mode)) {
g_set_error(error, EXCHANGE_FORMAT_ERROR,
EXCHANGE_FORMAT_ERROR_UNSUPPORTED_FILE_TYPE,
"Only regular files are supported: '%s'", filename);
return -1;
}
if (pv_file_seek(file, sizeof(exchange_format_v1_hdr_t), SEEK_SET, error))
return -1;
if (ctx->serialized_arcb) {
hdr.serialized_arcb = add_g_bytes(ctx->serialized_arcb, file, error);
if (*error)
return -1;
file_size += g_bytes_get_size(ctx->serialized_arcb);
}
if (ctx->measurement) {
hdr.measurement = add_g_bytes(ctx->measurement, file, error);
if (*error)
return -1;
file_size += g_bytes_get_size(ctx->measurement);
} else {
hdr.measurement.size = GUINT32_TO_BE(ctx->req_meas_size);
}
if (ctx->additional_data) {
hdr.additional_data = add_g_bytes(ctx->additional_data, file, error);
if (*error)
return -1;
file_size += g_bytes_get_size(ctx->additional_data);
} else {
hdr.additional_data.size = GUINT32_TO_BE(ctx->req_add_size);
}
if (ctx->user_data) {
tmp_size = g_bytes_get_size(ctx->user_data);
g_assert(tmp_size <= PVATTEST_USER_DATA_MAX_SIZE);
tmp_size = MIN(tmp_size, PVATTEST_USER_DATA_MAX_SIZE); /* should be a noop */
hdr.user_data = add_g_bytes(ctx->user_data, file, error);
if (*error)
return -1;
file_size += g_bytes_get_size(ctx->user_data);
}
if (ctx->config_uid) {
tmp_size = g_bytes_get_size(ctx->config_uid);
g_assert(tmp_size == PVATTEST_UID_SIZE);
tmp_size = MIN(tmp_size, PVATTEST_UID_SIZE); /* should be a noop */
hdr.config_uid = add_g_bytes(ctx->config_uid, file, error);
if (*error)
return -1;
file_size += g_bytes_get_size(ctx->config_uid);
}
/*
* This case should never happen. It could be seen as a programming error as:
* ARCB is restricted by kernel (and this tool) to be max 1M, Additional+meas to max 8pages
* userdata to 256B and config uid to 16b this is way less than 4G.
*
* However, lets be conservative and trow an error instead of an assertion.
*/
if (file_size > UINT32_MAX) {
g_set_error(
error, EXCHANGE_FORMAT_ERROR, EXCHANGE_FORMAT_ERROR_INVALID_FORMAT,
"The exchange file format cannot handle this much data in one blob. (%#lx bytes)",
file_size);
return -1;
}
hdr.size = GUINT32_TO_BE((uint32_t)file_size);
if (pv_file_seek(file, 0, SEEK_SET, error) != 0)
return -1;
if (sizeof(hdr) != pv_file_write(file, &hdr, sizeof(hdr), error))
return -1;
if (pv_file_seek(file, 0, SEEK_END, error) != 0)
return -1;
actual_file_size = pv_file_tell(file, error);
if (actual_file_size < 0)
return -1;
if (actual_file_size != (uint32_t)file_size) {
g_set_error(
error, EXCHANGE_FORMAT_ERROR, EXCHANGE_FORMAT_ERROR_WRITE,
"The exchange file size doesn't match the expectations: %ld bytes != %lu bytes",
actual_file_size, file_size);
return -1;
}
return 0;
}
static void print_entry(const char *name, GBytes *data, const gboolean print_data, FILE *stream)
{
if (!data)
return;
fprintf(stream, _("%s (%#lx bytes)"), name, g_bytes_get_size(data));
if (print_data) {
fprintf(stream, ":\n");
pvattest_hexdump(stream, data, 16, " ", TRUE);
}
fprintf(stream, "\n");
}
void exchange_info_print(const exchange_format_ctx_t *ctx, const gboolean print_data, FILE *stream)
{
pv_wrapped_g_assert(ctx);
pv_wrapped_g_assert(stream);
fprintf(stream, _("Version: %#x\n"), ctx->version);
fprintf(stream, _("Sections:\n"));
print_entry(_(" ARCB"), ctx->serialized_arcb, print_data, stream);
print_entry(_(" Measurement"), ctx->measurement, print_data, stream);
print_entry(_(" Additional Data"), ctx->additional_data, print_data, stream);
print_entry(_(" User Data"), ctx->user_data, print_data, stream);
print_entry(_(" Config UID"), ctx->config_uid, print_data, stream);
if (!ctx->measurement)
fprintf(stream, _("Required measurement size: %#x\n"), ctx->req_meas_size);
if (!ctx->additional_data)
fprintf(stream, _("Required additional data size: %#x\n"), ctx->req_add_size);
}

View File

@@ -1,166 +0,0 @@
/*
* Definitions for the pvattest exchange format to send attestation requests and responses between
* machines. The "exchange format" is a simple file format to send labeled binary blobs between
* pvattest instances on different machines. All sizes, etc are in big endian.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef PVATTEST_EXCHANGE_FORMAT_H
#define PVATTEST_EXCHANGE_FORMAT_H
/* Must be included before any other header */
#include "config.h"
#include "libpv/glib-helper.h"
#include "types.h"
#include "common.h"
/* Similar to linux/arch/s390x/include/uapi/uvdevice.h as this part needs to be
* architecture independent.
*/
#define PVATTEST_UID_SIZE 0x10UL
#define PVATTEST_USER_DATA_MAX_SIZE 0x100UL
#define PVATTEST_ARCB_MAX_SIZE 0x100000
#define PVATTEST_MEASUREMENT_MAX_SIZE 0x8000
#define PVATTEST_ADDITIONAL_MAX_SIZE 0x8000
#define PVATTEST_EXCHANGE_V_INVALID 0
#define PVATTEST_EXCHANGE_VERSION_1_00 0x0100
typedef struct _exchange_format_v1_hdr exchange_format_v1_hdr_t;
typedef struct _exchange_format_ctx exchange_format_ctx_t;
/**
* exchange_ctx_new:
*
* @version: Format version. Currently, only version 1 supported.
* @serialized_arcb: ARCB as #GBytes
* @req_measurement_size: Measurement size the given ARCB needs.
* @req_measurement_size: Additional Data size the given ARCB needs.
* @error: GError. *error will != NULL if error occurs.
*
* Returns: (nullable) (transfer full): new, empty exchange format context
*
*/
exchange_format_ctx_t *exchange_ctx_new(const uint32_t version, GBytes *serialized_arcb,
const uint32_t req_measurement_size,
const uint32_t req_additional_size, GError **error)
PV_NONNULL(2);
/**
* exchange_ctx_from_file:
*
* @filename: name of the file to be loaded
* @error: GError. *error will != NULL if error occurs.
*
* Loads all blobs from file and caches them in the context structure.
*
* Returns: (nullable) (transfer full): exchange format context filled with data from file
*
*/
exchange_format_ctx_t *exchange_ctx_from_file(const char *filename, GError **error) PV_NONNULL(1);
void clear_free_exchange_ctx(exchange_format_ctx_t *ctx);
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(exchange_format_ctx_t, clear_free_exchange_ctx)
/**
* exchange_set_serialized_arcb:
*
* @ctx: exchange format context
* @serialized_arcb: blob to add.
*
* Adds blob to the exchange format. Unreferences old data if already set.
*/
void exchange_set_serialized_arcb(exchange_format_ctx_t *ctx, GBytes *serialized_arcb)
PV_NONNULL(1, 2);
/**
* exchange_set_measurement:
*
* @ctx: exchange format context
* @measurement: blob to add.
*
* Adds blob to the exchange format. Unreferences old data if already set.
*/
void exchange_set_measurement(exchange_format_ctx_t *ctx, GBytes *measurement) PV_NONNULL(1, 2);
/**
* exchange_set_additional_data:
*
* @ctx: exchange format context
* @additional_data: blob to add.
*
* Adds blob to the exchange format. Unreferences old data if already set.
*/
void exchange_set_additional_data(exchange_format_ctx_t *ctx, GBytes *additional_data)
PV_NONNULL(1, 2);
/**
* exchange_set_user_data:
*
* @ctx: exchange format context
* @user_data: blob to add.
*
* Adds blob to the exchange format. Unreferences old data if already set.
*/
void exchange_set_user_data(exchange_format_ctx_t *ctx, GBytes *user_data) PV_NONNULL(1, 2);
/**
* exchange_set_config_uid:
*
* @ctx: exchange format context
* @config_uid: blob to add.
*
* Adds blob to the exchange format. Unreferences old data if already set.
*/
void exchange_set_config_uid(exchange_format_ctx_t *ctx, GBytes *config_uid) PV_NONNULL(1, 2);
GBytes *exchange_get_serialized_arcb(const exchange_format_ctx_t *ctx) PV_NONNULL(1);
GBytes *exchange_get_measurement(const exchange_format_ctx_t *ctx) PV_NONNULL(1);
GBytes *exchange_get_additional_data(const exchange_format_ctx_t *ctx) PV_NONNULL(1);
GBytes *exchange_get_user_data(const exchange_format_ctx_t *ctx) PV_NONNULL(1);
GBytes *exchange_get_config_uid(const exchange_format_ctx_t *ctx) PV_NONNULL(1);
uint32_t exchange_get_requested_measurement_size(const exchange_format_ctx_t *ctx) PV_NONNULL(1);
uint32_t exchange_get_requested_additional_data_size(const exchange_format_ctx_t *ctx)
PV_NONNULL(1);
/**
* exchange_write_to_file:
*
* @ctx: exchange format context
* @filename: name of the file to be loaded
* @error: GError. *error will != NULL if error occours.
*
* Takes all Data in the context and writes them into a file.
* Places the exchange format header before the data.
*
* Returns: 0 in case of success, -1 otherwise.
*/
int exchange_write_to_file(const exchange_format_ctx_t *ctx, const char *filename, GError **error)
PV_NONNULL(1, 2);
/**
* exchange_info_print:
*
* @ctx: exchange format context
* @print_data: TRUE: print present data + label names
* FALSE: just print label names of present data
* @stream: FILE* stream to print data
*
* Prints the content of @ctx to @stream.
*/
void exchange_info_print(const exchange_format_ctx_t *ctx, const gboolean print_data, FILE *stream)
PV_NONNULL(1, 3);
#define EXCHANGE_FORMAT_ERROR g_quark_from_static_string("pv-exchange-format_error-quark")
typedef enum {
EXCHANGE_FORMAT_ERROR_INVALID_FORMAT,
EXCHANGE_FORMAT_ERROR_UNSUPPORTED_VERSION,
EXCHANGE_FORMAT_ERROR_WRITE,
EXCHANGE_FORMAT_ERROR_UNSUPPORTED_FILE_TYPE,
} exchange_error_e;
#endif /* PVATTEST_EXCHANGE_FORMAT_H */

View File

@@ -1,213 +0,0 @@
/*
* Functions used for logging.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include "types.h"
#include "common.h"
#include "log.h"
void pvattest_log_increase_log_lvl(int *log_lvl)
{
if (*log_lvl >= PVATTEST_LOG_LVL_MAX)
return;
*log_lvl = *log_lvl << 1;
}
void pvattest_log_error(const char *format, ...)
{
va_list argp;
va_start(argp, format);
g_logv(NULL, PVATTEST_LOG_LVL_ERROR, format, argp);
va_end(argp);
}
void pvattest_log_warning(const char *format, ...)
{
va_list argp;
va_start(argp, format);
g_logv(NULL, PVATTEST_LOG_LVL_WARNING, format, argp);
va_end(argp);
}
void pvattest_log_info(const char *format, ...)
{
va_list argp;
va_start(argp, format);
g_logv(NULL, PVATTEST_LOG_LVL_INFO, format, argp);
va_end(argp);
}
void pvattest_log_debug(const char *format, ...)
{
va_list argp;
va_start(argp, format);
g_logv(NULL, PVATTEST_LOG_LVL_DEBUG, format, argp);
va_end(argp);
}
static void _log_print(FILE *stream, const char *prefix, const char *message, const char *postfix)
{
g_autofree char *prefix_empty = NULL, *new_msg = NULL;
size_t prefix_len = strlen(prefix);
char **message_v;
if (!prefix || prefix_len == 0) {
printf("%s%s", message, postfix);
return;
}
message_v = g_strsplit(message, "\n", 0);
prefix_empty = g_malloc0(prefix_len + 2);
snprintf(prefix_empty, prefix_len + 2, "\n%*c\b", (int)prefix_len, ' ');
new_msg = g_strjoinv(prefix_empty, message_v);
fprintf(stream, "%s%s%s", prefix, new_msg, postfix);
g_strfreev(message_v);
}
static void _log_logger(GLogLevelFlags level, const char *message, int log_level,
gboolean use_prefix, const char *postfix)
{
const char *prefix = "";
/* filter out messages depending on debugging level */
if ((level & PVATTEST_LOG_LVL_DEBUG) && log_level < PVATTEST_LOG_LVL_DEBUG)
return;
if ((level & PVATTEST_LOG_LVL_INFO) && log_level < PVATTEST_LOG_LVL_INFO)
return;
if (use_prefix && level & (G_LOG_LEVEL_WARNING | PVATTEST_LOG_LVL_WARNING))
prefix = _("WARNING: ");
if (use_prefix && level & (G_LOG_LEVEL_ERROR | PVATTEST_LOG_LVL_ERROR))
prefix = _("ERROR: ");
if (use_prefix && level & (G_LOG_LEVEL_DEBUG | PVATTEST_LOG_LVL_DEBUG))
prefix = _("DEBUG: ");
if (level & (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | PVATTEST_LOG_LVL_WARNING |
PVATTEST_LOG_LVL_ERROR))
_log_print(stderr, prefix, message, postfix);
else
_log_print(stdout, prefix, message, postfix);
}
/**
* prefixes type. and adds a "\n" ad the end.
*/
void pvattest_log_default_logger(const char *log_domain G_GNUC_UNUSED, GLogLevelFlags level,
const char *message, void *user_data)
{
int log_level = *(int *)user_data;
_log_logger(level, message, log_level, TRUE, "\n");
}
/*
* writes message as it is if log level is high enough.
*/
void pvattest_log_plain_logger(const char *log_domain G_GNUC_UNUSED, GLogLevelFlags level,
const char *message, void *user_data)
{
int log_level = *(int *)user_data;
_log_logger(level, message, log_level, FALSE, "");
}
void pvattest_log_bytes(const void *data, size_t size, size_t width, const char *prefix,
gboolean beautify, GLogLevelFlags log_lvl)
{
const uint8_t *data_b = data;
pv_wrapped_g_assert(data);
if (beautify)
g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "%s0x0000 ", prefix);
else
g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "%s", prefix);
for (size_t i = 0; i < size; i++) {
g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "%02x", data_b[i]);
if (i % 2 == 1 && beautify)
g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, " ");
if (i == size - 1)
break;
if (i % width == width - 1) {
if (beautify)
g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "\n%s0x%04lx ", prefix,
i + 1);
else
g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "\n%s", prefix);
}
}
g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "\n");
}
int pvattest_hexdump(FILE *stream, GBytes *bytes, const size_t width, const char *prefix,
const gboolean beautify)
{
const uint8_t *data;
size_t size;
pv_wrapped_g_assert(bytes);
pv_wrapped_g_assert(stream);
data = g_bytes_get_data(bytes, &size);
pv_wrapped_g_assert(data);
if (beautify) {
if (fprintf(stream, "%s0x0000 ", prefix) < 0)
return -1;
} else {
if (fprintf(stream, "%s", prefix) < 0)
return -1;
}
for (size_t i = 0; i < size; i++) {
if (fprintf(stream, "%02x", data[i]) < 0)
return -1;
if (i % 2 == 1 && beautify) {
if (fprintf(stream, " ") < 0)
return -1;
}
if (i == size - 1)
break;
if (width == 0)
continue;
if (i % width == width - 1) {
if (beautify) {
if (fprintf(stream, "\n%s0x%04lx ", prefix, i + 1) < 0)
return -1;
} else {
if (fprintf(stream, "\n%s", prefix) < 0)
return -1;
}
}
}
return 0;
}
void pvattest_log_GError(const char *info, GError *error)
{
pv_wrapped_g_assert(info);
if (!error)
return;
pvattest_log_error("%s:\n%s", info, error->message);
}

View File

@@ -1,67 +0,0 @@
/*
* Definitions used for logging.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef PVATTEST_LOG_H
#define PVATTEST_LOG_H
/* Must be included before any other header */
#include "config.h"
#include "libpv/glib-helper.h"
#include "libpv/macros.h"
#define PVATTEST_LOG_LVL_TOOL_ALL (1 << (G_LOG_LEVEL_USER_SHIFT))
#define PVATTEST_LOG_LVL_ERROR (1 << (G_LOG_LEVEL_USER_SHIFT))
#define PVATTEST_LOG_LVL_WARNING (1 << (G_LOG_LEVEL_USER_SHIFT + 1))
#define PVATTEST_LOG_LVL_INFO (1 << (G_LOG_LEVEL_USER_SHIFT + 2))
#define PVATTEST_LOG_LVL_DEBUG (1 << (G_LOG_LEVEL_USER_SHIFT + 3))
#define PVATTEST_LOG_LVL_DEFAULT PVATTEST_LOG_LVL_WARNING
#define PVATTEST_LOG_LVL_MAX PVATTEST_LOG_LVL_DEBUG
#define PVATTEST_BYTES_LOG_DOMAIN "pvattest_bytes"
void pvattest_log_increase_log_lvl(int *log_lvl);
void pvattest_log_error(const char *format, ...);
void pvattest_log_warning(const char *format, ...);
void pvattest_log_info(const char *format, ...);
void pvattest_log_debug(const char *format, ...);
/** pvattest_log_default_logger:
*
* A #GLogFunc implementation.
* Prefixes log level and adds a "\n" ad the end.
*/
void pvattest_log_default_logger(const char *log_domain, GLogLevelFlags level, const char *message,
void *user_data);
/* pvattest_log_plain_logger:
*
* A #GLogFunc implementation.
* Writes message as it is if log level is high enough.
*/
void pvattest_log_plain_logger(const char *log_domain, GLogLevelFlags level, const char *message,
void *user_data);
#define dhexdump(v, s) \
{ \
pvattest_log_debug("%s (%li byte):", #v, s); \
pvattest_log_bytes(v, s, 16L, " ", TRUE, PVATTEST_LOG_LVL_DEBUG); \
g_log(PVATTEST_BYTES_LOG_DOMAIN, PVATTEST_LOG_LVL_DEBUG, "\n"); \
}
#define gbhexdump(v) \
{ \
pvattest_log_debug("%s:(%li byte):", #v, g_bytes_get_size(v)); \
pvattest_log_bytes(g_bytes_get_data(v, NULL), g_bytes_get_size(v), 16L, " ", \
TRUE, PVATTEST_LOG_LVL_DEBUG); \
g_log(PVATTEST_BYTES_LOG_DOMAIN, PVATTEST_LOG_LVL_DEBUG, "\n"); \
}
void pvattest_log_bytes(const void *data, size_t size, size_t width, const char *prefix,
gboolean beautify, GLogLevelFlags log_lvl) PV_NONNULL(1);
int pvattest_hexdump(FILE *stream, GBytes *bytes, const size_t width, const char *prefix,
const gboolean beautify) PV_NONNULL(1, 2);
void pvattest_log_GError(const char *info, GError *error) PV_NONNULL(1);
#endif /* PVATTEST_LOG_H */

View File

@@ -1,466 +0,0 @@
/*
* Entry point for the pvattest tool.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <openssl/evp.h>
#include "libpv/crypto.h"
#include "libpv/cert.h"
#include "uvio.h"
#include "common.h"
#include "attestation.h"
#include "arcb.h"
#include "argparse.h"
#include "exchange_format.h"
#include "log.h"
#define PVATTEST_NID NID_secp521r1
#define PVATTEST_UV_PATH "/dev/uv"
#define PVATTEST_EXIT_MEASURE_NOT_VERIFIED 2
enum pvattest_error {
PVATTEST_ERROR_INVAL_ATT_RESULT,
};
static arcb_v1_t *create_arcb(char **host_key_paths, const gboolean use_nonce,
const gboolean phkh_img, const gboolean phkh_att,
const uint64_t user_paf, GError **error)
{
g_autoptr(GBytes) arpk = NULL, meas_key = NULL, nonce = NULL, iv = NULL;
g_autoslist(PvX509WithPath) host_keys_with_path = NULL;
g_autoslist(EVP_PKEY) evp_host_keys = NULL;
const uint32_t mai = MAI_HMAC_SHA512;
g_autoptr(EVP_PKEY) evp_cpk = NULL;
g_autoptr(arcb_v1_t) arcb = NULL;
uint64_t paf = user_paf;
g_assert(host_key_paths);
arpk = pv_generate_key(EVP_aes_256_gcm(), error);
if (!arpk)
return NULL;
iv = pv_generate_iv(EVP_aes_256_gcm(), error);
if (!iv)
return NULL;
evp_cpk = pv_generate_ec_key(PVATTEST_NID, error);
if (!evp_cpk)
return NULL;
meas_key = pv_generate_rand_data(HMAC_SHA512_KEY_SIZE, error);
if (!meas_key)
return NULL;
if (phkh_img)
paf |= ARCB_V1_PAF_AAD_PHKH_HEADER;
if (phkh_att)
paf |= ARCB_V1_PAF_AAD_PHKH_ATTEST;
arcb = arcb_v1_new(arpk, iv, mai, evp_cpk, meas_key, paf, error);
if (!arcb)
return NULL;
if (use_nonce) {
nonce = pv_generate_rand_data(ARCB_V1_NONCE_SIZE, error);
if (!nonce)
return NULL;
arcb_v1_set_nonce(arcb, nonce);
}
host_keys_with_path = pv_load_certificates(host_key_paths, error);
if (!host_keys_with_path)
return NULL;
/* Extract EVP_PKEY structures and verify that the correct elliptic
* curve is used.
*/
evp_host_keys = pv_get_ec_pubkeys(host_keys_with_path, PVATTEST_NID, error);
if (!evp_host_keys)
return NULL;
for (GSList *iter = evp_host_keys; iter; iter = iter->next) {
EVP_PKEY *host_key = iter->data;
if (arcb_v1_add_key_slot(arcb, host_key, error) < 0)
return NULL;
}
return g_steal_pointer(&arcb);
}
#define __PVATTEST_CREATE_ERROR_MSG _("Creating the attestation request failed")
static int do_create(const pvattest_create_config_t *create_config)
{
g_autoptr(exchange_format_ctx_t) output_ctx = NULL;
uint32_t measurement_size, additional_data_size;
g_autoptr(GBytes) serialized_arcb = NULL;
g_autoptr(arcb_v1_t) arcb = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GBytes) arpk = NULL;
if (!create_config->use_nonce)
pvattest_log_warning(_("No nonce used. (Experimental setting)"));
if (create_config->no_verify) {
pvattest_log_warning(_("Host-key document verification is disabled.\n"
"The attestation result could be compromised!"));
pvattest_log_debug(_("Verification skipped."));
} else {
if (pv_verify_host_key_docs_by_path(
create_config->host_key_document_paths, create_config->root_ca_path,
create_config->crl_paths, create_config->certificate_paths,
create_config->online, &error) < 0)
goto err_exit;
pvattest_log_debug(_("Verification passed."));
}
/* build attestation request */
arcb = create_arcb(create_config->host_key_document_paths, create_config->use_nonce,
create_config->phkh_img, create_config->phkh_att, create_config->paf,
&error);
if (!arcb)
goto err_exit;
additional_data_size = arcb_v1_get_required_additional_size(arcb);
if (create_config->x_aad_size >= 0) {
g_assert_cmpint(create_config->x_aad_size, <=, UINT32_MAX);
additional_data_size = (uint32_t)create_config->x_aad_size;
}
measurement_size = arcb_v1_get_required_measurement_size(arcb, &error);
if (error)
goto err_exit;
serialized_arcb = arcb_v1_serialize(arcb, &error);
if (!serialized_arcb)
goto err_exit;
/* write attestation request data to file */
output_ctx = exchange_ctx_new(PVATTEST_EXCHANGE_VERSION_1_00, serialized_arcb,
measurement_size, additional_data_size, &error);
if (!output_ctx)
goto err_exit;
if (exchange_write_to_file(output_ctx, create_config->output_path, &error) < 0)
goto err_exit;
pvattest_log_debug(_("ARCB written to file."));
/* write attestation request protection key to file */
arpk = arcb_v1_get_arp_key(arcb);
wrapped_g_file_set_content(create_config->arp_key_out_path, arpk, 0600, &error);
if (error)
goto err_exit;
pvattest_log_debug(_("ARPK written to file."));
return EXIT_SUCCESS;
err_exit:
pvattest_log_GError(__PVATTEST_CREATE_ERROR_MSG, error);
return EXIT_FAILURE;
}
#ifdef PVATTEST_COMPILE_PERFORM
#define __PVATTEST_MEASURE_ERROR_MSG _("Performing the attestation measurement failed")
static int do_perform(pvattest_perform_config_t *perform_config)
{
g_autoptr(GBytes) serialized_arcb = NULL, user_data = NULL, measurement = NULL,
additional_data = NULL, config_uid = NULL;
size_t uv_measurement_data_size, uv_addidtional_data_size;
g_autoptr(exchange_format_ctx_t) exchange_ctx = NULL;
uint32_t measurement_size, additional_data_size;
g_autoptr(uvio_attest_t) uvio_attest = NULL;
g_autoptr(GError) error = NULL;
be16_t uv_rc;
int uv_fd;
exchange_ctx = exchange_ctx_from_file(perform_config->input_path, &error);
if (!exchange_ctx)
goto err_exit;
serialized_arcb = exchange_get_serialized_arcb(exchange_ctx);
if (!serialized_arcb) {
g_set_error(&error, PVATTEST_ERROR, ARCB_ERR_INVALID_ARCB,
_("The input does not provide an attestation request."));
goto err_exit;
}
measurement_size = exchange_get_requested_measurement_size(exchange_ctx);
additional_data_size = exchange_get_requested_additional_data_size(exchange_ctx);
pvattest_log_debug(_("Input data loaded."));
if (perform_config->user_data_path) {
user_data = pv_file_get_content_as_g_bytes(perform_config->user_data_path, &error);
if (!user_data)
goto err_exit;
pvattest_log_debug(_("Added user data from '%s'"), perform_config->user_data_path);
}
uvio_attest = build_attestation_v1_ioctl(serialized_arcb, user_data, measurement_size,
additional_data_size, &error);
if (!uvio_attest)
goto err_exit;
pvattest_log_debug(_("attestation context generated."));
/* execute attestation */
uv_fd = uvio_open(PVATTEST_UV_PATH, &error);
if (uv_fd < 0)
goto err_exit;
uv_rc = uvio_ioctl_attest(uv_fd, uvio_attest, &error);
close(uv_fd);
if (uv_rc != UVC_EXECUTED)
goto err_exit;
pvattest_log_debug(_("attestation measurement successful. rc = %#x"), uv_rc);
/* write to file */
measurement = uvio_get_measurement(uvio_attest);
additional_data = uvio_get_additional_data(uvio_attest);
config_uid = uvio_get_config_uid(uvio_attest);
uv_measurement_data_size = measurement == NULL ? 0 : g_bytes_get_size(measurement);
if (uv_measurement_data_size != measurement_size) {
g_set_error(&error, PVATTEST_ERROR, PVATTEST_ERROR_INVAL_ATT_RESULT,
"The measurement size returned by Ultravisor is not as expected.");
goto err_exit;
}
uv_addidtional_data_size = additional_data == NULL ? 0 : g_bytes_get_size(additional_data);
if (uv_addidtional_data_size != additional_data_size) {
g_set_error(&error, PVATTEST_ERROR, PVATTEST_ERROR_INVAL_ATT_RESULT,
"The additional data size returned by Ultravisor is not as expected.");
goto err_exit;
}
exchange_set_measurement(exchange_ctx, measurement);
if (additional_data)
exchange_set_additional_data(exchange_ctx, additional_data);
exchange_set_config_uid(exchange_ctx, config_uid);
if (user_data)
exchange_set_user_data(exchange_ctx, user_data);
if (exchange_write_to_file(exchange_ctx, perform_config->output_path, &error) < 0)
goto err_exit;
pvattest_log_debug(_("Output written to file."));
return EXIT_SUCCESS;
err_exit:
pvattest_log_GError(__PVATTEST_MEASURE_ERROR_MSG, error);
return EXIT_FAILURE;
}
#endif /* PVATTEST_COMPILE_PERFORM */
static int fprint_verify_result(FILE *stream, const enum verify_output_format fmt,
GBytes *config_uid, GBytes *additional_data)
{
switch (fmt) {
case VERIFY_FMT_HUMAN:
if (fprintf(stream, _("Attestation measurement verified\n")) < 0)
return -1;
if (fprintf(stream, _("Config UID:\n")) < 0)
return -1;
if (pvattest_hexdump(stream, config_uid, 0x10L, "0x", FALSE) < 0)
return -1;
if (fprintf(stream, _("\n")) < 0)
return -1;
if (additional_data) {
if (fprintf(stream, _("Additional Data:\n")) < 0)
return -1;
if (pvattest_hexdump(stream, additional_data, 0x60L, "0x", FALSE) < 0)
return -1;
if (fprintf(stream, _("\n")) < 0)
return -1;
}
break;
case VERIFY_FMT_YAML:
if (fprintf(stream, "cuid: ") < 0)
return -1;
if (pvattest_hexdump(stream, config_uid, 0L, "'0x", FALSE) < 0)
return -1;
if (fprintf(stream, _("'\n")) < 0)
return -1;
if (additional_data) {
if (fprintf(stream, "add: ") < 0)
return -1;
if (pvattest_hexdump(stream, additional_data, 0x0L, "'0x", FALSE) < 0)
return -1;
if (fprintf(stream, _("'\n")) < 0)
return -1;
}
break;
default:
g_assert_not_reached();
break;
}
return 0;
}
#define __PVATTEST_VERIFY_ERROR_MSG _("Attestation measurement verification failed")
static int do_verify(const pvattest_verify_config_t *verify_config, const int appl_log_lvl)
{
g_autoptr(GBytes) user_data = NULL, uv_measurement = NULL, additional_data = NULL,
image_hdr = NULL, calc_measurement = NULL, config_uid = NULL,
meas_key = NULL, arp_key = NULL, nonce = NULL, serialized_arcb = NULL;
g_autofree att_meas_ctx_t *measurement_hdr = NULL;
g_autoptr(exchange_format_ctx_t) input_ctx = NULL;
const char *err_prefix = __PVATTEST_VERIFY_ERROR_MSG;
g_autoptr(GError) error = NULL;
gboolean rc;
image_hdr = pv_file_get_content_as_g_bytes(verify_config->hdr_path, &error);
if (!image_hdr)
goto err_exit;
measurement_hdr = att_extract_from_hdr(image_hdr, &error);
if (!measurement_hdr)
goto err_exit;
pvattest_log_debug(_("Image header loaded."));
input_ctx = exchange_ctx_from_file(verify_config->input_path, &error);
if (!input_ctx)
goto err_exit;
config_uid = exchange_get_config_uid(input_ctx);
uv_measurement = exchange_get_measurement(input_ctx);
user_data = exchange_get_user_data(input_ctx);
additional_data = exchange_get_additional_data(input_ctx);
serialized_arcb = exchange_get_serialized_arcb(input_ctx);
if (!uv_measurement || !serialized_arcb) {
g_set_error(&error, PVATTEST_ERROR, PVATTEST_SUBC_INVALID,
_("Input data has no measurement"));
goto err_exit;
}
pvattest_log_debug(_("Input data loaded."));
att_add_uid(measurement_hdr, config_uid);
arp_key = pv_file_get_content_as_g_bytes(verify_config->arp_key_in_path, &error);
if (!arp_key)
goto err_exit;
pvattest_log_debug(_("ARPK loaded."));
rc = arcb_v1_verify_serialized_arcb(serialized_arcb, arp_key, &meas_key, &nonce, &error);
if (!rc)
goto err_exit;
pvattest_log_debug(_("Input ARCB verified."));
calc_measurement = att_gen_measurement_hmac_sha512(measurement_hdr, meas_key, user_data,
nonce, additional_data, &error);
if (!calc_measurement)
goto err_exit;
pvattest_log_debug(_("Measurement calculated."));
if (!att_verify_measurement(calc_measurement, uv_measurement, &error)) {
pvattest_log_GError(__PVATTEST_VERIFY_ERROR_MSG, error);
pvattest_log_debug(_("Measurement values:"));
gbhexdump(uv_measurement);
gbhexdump(calc_measurement);
return PVATTEST_EXIT_MEASURE_NOT_VERIFIED;
}
/* Write human-readable output to stdout */
if (appl_log_lvl >= PVATTEST_LOG_LVL_INFO) {
if (fprint_verify_result(stdout, VERIFY_FMT_HUMAN, config_uid, additional_data) <
0) {
g_set_error(&error, PV_GLIB_HELPER_ERROR, PV_GLIB_HELPER_FILE_ERROR,
"stdout: %s", g_strerror(errno));
err_prefix = "Failed to write output";
goto err_exit;
}
}
/* Write to file */
if (verify_config->output_path) {
g_autoptr(FILE) output = pv_file_open(verify_config->output_path, "wx", &error);
if (!output) {
err_prefix = "Failed to write output";
goto err_exit;
}
if (fprint_verify_result(output, verify_config->output_fmt, config_uid,
additional_data) < 0) {
g_set_error(&error, PV_GLIB_HELPER_ERROR, PV_GLIB_HELPER_FILE_ERROR,
"'%s': %s", verify_config->output_path, g_strerror(errno));
err_prefix = "Failed to write output";
goto err_exit;
}
}
return EXIT_SUCCESS;
err_exit:
pvattest_log_GError(err_prefix, error);
return EXIT_FAILURE;
}
/*
* Will not free the config structs, but the nested char* etc.
* that's what we need to do as we will receive a statically allocated config_t
* Not defined in the parse header as someone might incorrectly assume
* that the config pointers will be freed.
*/
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(pvattest_config_t, pvattest_parse_clear_config)
int main(int argc, char *argv[])
{
int appl_log_lvl = PVATTEST_LOG_LVL_DEFAULT;
g_autoptr(pvattest_config_t) config = NULL;
g_autoptr(GError) error = NULL;
enum pvattest_command command;
int rc;
/* setting up the default log handler to filter messages based on the
* log level specified by the user.
*/
g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
&pvattest_log_default_logger, &appl_log_lvl);
/* setting up the log handler for hexdumps (no prefix and '\n' at end of
* message)to filter messages based on the log level specified by the
* user.
*/
g_log_set_handler(PVATTEST_BYTES_LOG_DOMAIN,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
&pvattest_log_plain_logger, &appl_log_lvl);
command = pvattest_parse(&argc, &argv, &config, &error);
if (command == PVATTEST_SUBC_INVALID) {
pvattest_log_error(_("%s\nTry '%s --help' for more information"), error->message,
GETTEXT_PACKAGE);
exit(EXIT_FAILURE);
}
g_assert(config);
appl_log_lvl = config->general.log_level;
pv_init();
switch (command) {
case PVATTEST_SUBC_CREATE:
rc = do_create(&config->create);
break;
#ifdef PVATTEST_COMPILE_PERFORM
case PVATTEST_SUBC_PERFORM:
rc = do_perform(&config->perform);
break;
#endif /* PVATTEST_COMPILE_PERFORM */
case PVATTEST_SUBC_VERIFY:
rc = do_verify(&config->verify, appl_log_lvl);
break;
default:
g_return_val_if_reached(EXIT_FAILURE);
}
pv_cleanup();
return rc;
}

View File

@@ -1,18 +0,0 @@
/*
* Common data type definitions and functions
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef PVATTEST_TYPES_H
#define PVATTEST_TYPES_H
#include <stdint.h>
/* Types to mark values as big endian. */
typedef uint16_t be16_t;
typedef uint32_t be32_t;
typedef uint64_t be64_t;
#endif /* PVATTEST_TYPES_H */

View File

@@ -1,182 +0,0 @@
/*
* UV device (uvio) related functions and definitions.
* uses s390 only (kernel) features.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
/* Must be included before any other header */
#include "config.h"
#include "lib/zt_common.h"
#ifdef PVATTEST_COMPILE_PERFORM
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "attestation.h"
#include "uvio.h"
#include "common.h"
#include "log.h"
/* some helper macros */
#define U64_TO_PTR(v) ((void *)(v))
#define PTR_TO_U64(ptr) ((uint64_t)(ptr))
uvio_attest_t *build_attestation_v1_ioctl(GBytes *serialized_arcb, GBytes *user_data,
const uint32_t measurement_size,
const uint32_t add_data_size, GError **error)
{
g_autoptr(uvio_attest_t) uvio_attest = NULL;
size_t arcb_size;
void *arcb;
pv_wrapped_g_assert(serialized_arcb);
g_bytes_ref(serialized_arcb);
arcb = g_bytes_unref_to_data(serialized_arcb, &arcb_size);
uvio_attest = g_malloc0(sizeof(*uvio_attest));
uvio_attest->arcb_addr = PTR_TO_U64(g_steal_pointer(&arcb));
g_assert_cmpuint(arcb_size, <, UINT32_MAX);
uvio_attest->arcb_len = (uint32_t)arcb_size;
/* transferred the local ownership of the arcb from this function to uvio_attest; nullify pointer */
g_steal_pointer(&serialized_arcb);
if (user_data) {
size_t copied_data_size;
if (pv_gbytes_memcpy(uvio_attest->user_data, sizeof(uvio_attest->user_data),
user_data, &copied_data_size) == NULL) {
g_set_error(error, ATT_ERROR, ATT_ERR_INVALID_USER_DATA,
_("User data %li bytes is larger than %li bytes"),
g_bytes_get_size(user_data), sizeof(uvio_attest->user_data));
return NULL;
}
STATIC_ASSERT(sizeof(uvio_attest->user_data) <= UINT16_MAX);
uvio_attest->user_data_len = (uint16_t)copied_data_size;
}
uvio_attest->meas_addr = PTR_TO_U64(g_malloc0(measurement_size));
uvio_attest->meas_len = measurement_size;
uvio_attest->add_data_addr = PTR_TO_U64(g_malloc0(add_data_size));
uvio_attest->add_data_len = add_data_size;
return g_steal_pointer(&uvio_attest);
}
void uvio_attest_free(uvio_attest_t *attest)
{
if (!attest)
return;
g_free(U64_TO_PTR(attest->arcb_addr));
g_free(U64_TO_PTR(attest->meas_addr));
g_free(U64_TO_PTR(attest->add_data_addr));
g_free(attest);
}
GBytes *uvio_get_measurement(const uvio_attest_t *attest)
{
pv_wrapped_g_assert(attest);
if (attest->meas_addr == (__u64)0)
return NULL;
return g_bytes_new(U64_TO_PTR(attest->meas_addr), attest->meas_len);
}
GBytes *uvio_get_additional_data(const uvio_attest_t *attest)
{
pv_wrapped_g_assert(attest);
if (attest->add_data_addr == (__u64)0)
return NULL;
return g_bytes_new(U64_TO_PTR(attest->add_data_addr), attest->add_data_len);
}
GBytes *uvio_get_config_uid(const uvio_attest_t *attest)
{
pv_wrapped_g_assert(attest);
return g_bytes_new(attest->config_uid, sizeof(attest->config_uid));
}
uint16_t uvio_ioctl(const int uv_fd, const unsigned int cmd, const uint32_t flags,
const void *argument, const uint32_t argument_size, GError **error)
{
g_autofree struct uvio_ioctl_cb *uv_ioctl = g_malloc0(sizeof(*uv_ioctl));
int rc, cached_errno;
pv_wrapped_g_assert(argument);
uv_ioctl->flags = flags;
uv_ioctl->argument_addr = PTR_TO_U64(argument);
uv_ioctl->argument_len = argument_size;
rc = ioctl(uv_fd, cmd, uv_ioctl);
cached_errno = errno;
if (rc < 0) {
g_set_error(error, UVIO_ERROR, UVIO_ERR_UV_IOCTL, _("ioctl failed: %s "),
g_strerror(cached_errno));
return 0;
}
if (uv_ioctl->uv_rc != UVC_EXECUTED)
g_set_error(error, UVIO_ERROR, UVIO_ERR_UV_NOT_OK,
_("Ultravisor call returned '%#x' (%s)"), uv_ioctl->uv_rc,
uvio_uv_rc_to_str(uv_ioctl->uv_rc));
return uv_ioctl->uv_rc;
}
uint16_t uvio_ioctl_attest(const int uv_fd, uvio_attest_t *attest, GError **error)
{
pv_wrapped_g_assert(attest);
return uvio_ioctl(uv_fd, UVIO_IOCTL_ATT, 0, attest, sizeof(*attest), error);
}
int uvio_open(const char *uv_path, GError **error)
{
pv_wrapped_g_assert(uv_path);
int uv_fd;
int cached_errno;
uv_fd = open(uv_path, O_RDWR);
cached_errno = errno;
if (uv_fd < 0)
g_set_error(error, UVIO_ERROR, UVIO_ERR_UV_OPEN,
_("Cannot open uv driver at %s: %s"), uv_path,
g_strerror(cached_errno));
return uv_fd;
}
const char *uvio_uv_rc_to_str(const int rc)
{
switch (rc) {
case 0x02:
return _("Invalid UV command");
case 0x106:
return _("Unsupported attestation request version");
case 0x108:
return _("Number of key slots is greater than the maximum number supported");
case 0x10a:
return _("Unsupported plaintext attestation flags");
case 0x10c:
return _(
"Unable to decrypt attestation request control block. No valid host-key was provided");
case 0x10d:
return _("Measurement data length is too small to store measurement");
case 0x10e:
return _("Additional data length is too small to store measurement");
default:
return _("Unknown code");
}
}
#endif /* PVATTEST_COMPILE_PERFORM */

View File

@@ -1,110 +0,0 @@
/*
* UV device (uvio) related functions and definitions.
*
* Copyright IBM Corp. 2022
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef PVATTEST_UVIO_H
#define PVATTEST_UVIO_H
#include "config.h"
#ifdef PVATTEST_COMPILE_PERFORM
#include <sys/ioctl.h>
#include <asm/uvdevice.h>
#include "libpv/glib-helper.h"
#include "arcb.h"
#include "common.h"
#define UVC_EXECUTED 0x0001
typedef struct uvio_attest uvio_attest_t;
G_STATIC_ASSERT(sizeof(uvio_attest_t) == 0x138);
G_STATIC_ASSERT(sizeof(struct uvio_ioctl_cb) == 0x40);
/**
* build_attestation_v1_ioctl:
* @serialized_arcb: A ARCB in binary format
* @user_data (optional): up to 256 bytes of user data to be added to the measurement
* @measurement_size: Size of the measurement result to be allocated
* @add_data_size: Size of the additional data to be allocated
* @error: return location for a #GError
*
* Builds the structure to be passed to `/dev/uv` for attestation IOCTLs and
* allocates any required memory.
*
* Returns: (nullable) (transfer full): Pointer to a uvio_attest_t to be passed to `/dev/uv`
*/
uvio_attest_t *build_attestation_v1_ioctl(GBytes *serialized_arcb, GBytes *user_data,
const uint32_t measurement_size,
const uint32_t add_data_size, GError **error)
PV_NONNULL(1);
GBytes *uvio_get_measurement(const uvio_attest_t *attest) PV_NONNULL(1);
GBytes *uvio_get_additional_data(const uvio_attest_t *attest) PV_NONNULL(1);
GBytes *uvio_get_config_uid(const uvio_attest_t *attest) PV_NONNULL(1);
void uvio_attest_free(uvio_attest_t *attest);
WRAPPED_G_DEFINE_AUTOPTR_CLEANUP_FUNC(uvio_attest_t, uvio_attest_free)
/**
* uvio_ioctl:
* @uv_fd: file descriptor to the UV-device
* @cmd: IOCTL cmd
* @flags: flags for the uv IOCTL
* @argument: pointer to the payload
* @argument_size: size of #argument
* @error: return location for a #GError
*
* Builds the IOCTL structure using, flags and argument, performs the IOCTL, and returns the UV rc in big endian.
* If the device driver emits an error code, a corresponding #GError will be created.
* Use the specialized calls (uvio_ioctl_*).
*
* Returns: UV rc if no device error occurred (>0)
* 0 on #GError
*/
uint16_t uvio_ioctl(const int uv_fd, const unsigned int cmd, const uint32_t flags,
const void *argument, const uint32_t argument_size, GError **error)
PV_NONNULL(4);
/**
* uvio_ioctl_attest:
* @uv_fd: file descriptor to the UV-device
* @attest: pointer to the attestation request
* @error: return location for a #GError
*
* Wraps 'uvio_ioctl' for attestation.
*
* Returns: UV rc if no device error occurred (>0)
* 0 on #GError
*/
uint16_t uvio_ioctl_attest(const int uv_fd, uvio_attest_t *attest, GError **error) PV_NONNULL(2);
/**
* uvio_open:
* @uv_path: path of the UV-device usually at /dev/uv
* @error: return location for a #GError
*
* Returns: File descriptor for the UV-device
* 0 on #GError
*/
int uvio_open(const char *uv_path, GError **error) PV_NONNULL(1);
/**
* uvio_uv_rc_to_str:
* @rc: UV return code
*
* Returns: Pointer to an error string corresponding to the given UV-rc.
*/
const char *uvio_uv_rc_to_str(const int rc);
#define UVIO_ERROR g_quark_from_static_string("pv-uvio_error-quark")
typedef enum {
UVIO_ERR_UV_IOCTL,
UVIO_ERR_UV_OPEN,
UVIO_ERR_UV_NOT_OK,
} uvio_error_e;
#endif /* PVATTEST_COMPILE_PERFORM */
#endif /* PVATTEST_UVIO_H */

734
rust/Cargo.lock generated
View File

@@ -2,21 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.2"
@@ -81,37 +66,12 @@ version = "1.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
[[package]]
name = "assert-json-diff"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "backtrace"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@@ -130,12 +90,6 @@ version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "bytes"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
[[package]]
name = "cc"
version = "1.0.79"
@@ -173,6 +127,15 @@ dependencies = [
"terminal_size",
]
[[package]]
name = "clap_complete"
version = "4.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6b5c519bab3ea61843a7923d074b04245624bb84a64a8c150f5deb014e388b"
dependencies = [
"clap",
]
[[package]]
name = "clap_derive"
version = "4.3.2"
@@ -208,15 +171,15 @@ dependencies = [
"openssl-probe",
"openssl-sys",
"schannel",
"socket2 0.4.9",
"socket2",
"winapi",
]
[[package]]
name = "curl-sys"
version = "0.4.68+curl-8.4.0"
version = "0.4.72+curl-8.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4a0d18d88360e374b16b2273c832b5e57258ffc1d4aa4f96b108e0738d5752f"
checksum = "29cbdc8314c447d11e8fd156dcdd031d9e02a7a976163e396b548c03153bc9ea"
dependencies = [
"cc",
"libc",
@@ -224,15 +187,9 @@ dependencies = [
"openssl-sys",
"pkg-config",
"vcpkg",
"windows-sys 0.48.0",
"windows-sys 0.52.0",
]
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.1"
@@ -254,12 +211,6 @@ dependencies = [
"libc",
]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foreign-types"
version = "0.3.2"
@@ -275,104 +226,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "form_urlencoded"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652"
dependencies = [
"percent-encoding",
]
[[package]]
name = "futures"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c"
[[package]]
name = "futures-executor"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa"
[[package]]
name = "futures-macro"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817"
[[package]]
name = "futures-task"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2"
[[package]]
name = "futures-util"
version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "getrandom"
version = "0.2.10"
@@ -384,43 +237,12 @@ dependencies = [
"wasi",
]
[[package]]
name = "gimli"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "h2"
version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d6250322ef6e60f93f9a2162799302cd6f68f79f6e5d85c8c16f14d1d958178"
dependencies = [
"bytes",
"fnv",
"futures-core",
"futures-sink",
"futures-util",
"http",
"indexmap 2.1.0",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.4.1"
@@ -433,64 +255,6 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "http"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb"
dependencies = [
"bytes",
"fnv",
"itoa",
]
[[package]]
name = "http-body"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1"
dependencies = [
"bytes",
"http",
"pin-project-lite",
]
[[package]]
name = "httparse"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904"
[[package]]
name = "httpdate"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "hyper"
version = "0.14.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffb1cfd654a8219eaef89881fdb3bb3b1cdc5fa75ded05d6933b2b382e395468"
dependencies = [
"bytes",
"futures-channel",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"socket2 0.4.9",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]]
name = "indexmap"
version = "1.9.3"
@@ -498,17 +262,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown 0.12.3",
]
[[package]]
name = "indexmap"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [
"equivalent",
"hashbrown 0.14.3",
"hashbrown",
]
[[package]]
@@ -570,16 +324,6 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519"
[[package]]
name = "lock_api"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.19"
@@ -592,63 +336,6 @@ version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "miniz_oxide"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
dependencies = [
"adler",
]
[[package]]
name = "mio"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2"
dependencies = [
"libc",
"wasi",
"windows-sys 0.48.0",
]
[[package]]
name = "mockito"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8d3038e23466858569c2d30a537f691fa0d53b51626630ae08262943e3bbb8b"
dependencies = [
"assert-json-diff",
"futures",
"hyper",
"log",
"rand",
"regex",
"serde_json",
"serde_urlencoded",
"similar",
"tokio",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "object"
version = "0.32.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.19.0"
@@ -699,58 +386,6 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "openssl_extensions"
version = "0.1.0"
dependencies = [
"foreign-types",
"libc",
"log",
"openssl",
"openssl-sys",
]
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "percent-encoding"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94"
[[package]]
name = "pin-project-lite"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.27"
@@ -772,66 +407,47 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "pv"
version = "1.0.0"
dependencies = [
"byteorder",
"clap",
"curl",
"log",
"mockito",
"once_cell",
"openssl",
"openssl_extensions",
"pv_core",
"serde",
"serde_test",
"thiserror",
"utils",
"zerocopy",
]
[[package]]
name = "pv_core"
version = "1.0.0"
dependencies = [
"byteorder",
"lazy_static",
"libc",
"log",
"mockito",
"serde",
"serde_test",
"thiserror",
"utils",
"zerocopy",
]
[[package]]
name = "pvapconfig"
version = "0.9.0"
version = "0.10.0"
dependencies = [
"clap",
"clap_complete",
"lazy_static",
"openssl",
"openssl-sys",
"pv_core",
"rand",
"regex",
"s390_pv_core",
"serde",
"serde_yaml",
"utils",
]
[[package]]
name = "pvattest"
version = "0.10.0"
dependencies = [
"anyhow",
"byteorder",
"clap",
"clap_complete",
"log",
"s390_pv",
"serde",
"serde_yaml",
"utils",
"zerocopy",
]
[[package]]
name = "pvsecret"
version = "0.9.0"
version = "0.10.0"
dependencies = [
"anyhow",
"clap",
"clap_complete",
"log",
"pv",
"s390_pv",
"serde_yaml",
"utils",
]
@@ -875,15 +491,6 @@ dependencies = [
"getrandom",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "regex"
version = "1.10.2"
@@ -913,12 +520,6 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rustc-demangle"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustix"
version = "0.37.27"
@@ -939,6 +540,37 @@ version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
[[package]]
name = "s390_pv"
version = "0.10.0"
dependencies = [
"byteorder",
"curl",
"foreign-types",
"log",
"openssl",
"openssl-sys",
"s390_pv_core",
"serde",
"serde_test",
"thiserror",
"zerocopy",
]
[[package]]
name = "s390_pv_core"
version = "0.10.0"
dependencies = [
"byteorder",
"lazy_static",
"libc",
"log",
"serde",
"serde_test",
"thiserror",
"zerocopy",
]
[[package]]
name = "schannel"
version = "0.1.21"
@@ -948,12 +580,6 @@ dependencies = [
"windows-sys 0.42.0",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.164"
@@ -974,17 +600,6 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46266871c240a00b8f503b877622fe33430b3c7d963bdc0f2adc511e54a1eae3"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_test"
version = "1.0.176"
@@ -994,61 +609,19 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
dependencies = [
"form_urlencoded",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.9.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9d684e3ec7de3bf5466b32bd75303ac16f0736426e5a4e0d6e489559ce1249c"
dependencies = [
"indexmap 1.9.3",
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]]
name = "similar"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2aeaf503862c419d66959f5d7ca015337d864e9c49485d771b732e2a20453597"
[[package]]
name = "slab"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
dependencies = [
"autocfg",
]
[[package]]
name = "smallvec"
version = "1.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970"
[[package]]
name = "socket2"
version = "0.4.9"
@@ -1059,16 +632,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "socket2"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e"
dependencies = [
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "strsim"
version = "0.10.0"
@@ -1116,81 +679,6 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio"
version = "1.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653"
dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"num_cpus",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2 0.5.4",
"tokio-macros",
"windows-sys 0.48.0",
]
[[package]]
name = "tokio-macros"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-util"
version = "0.7.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15"
dependencies = [
"bytes",
"futures-core",
"futures-sink",
"pin-project-lite",
"tokio",
"tracing",
]
[[package]]
name = "tower-service"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
[[package]]
name = "tracing"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [
"pin-project-lite",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [
"once_cell",
]
[[package]]
name = "try-lock"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed"
[[package]]
name = "unicode-ident"
version = "1.0.9"
@@ -1211,7 +699,14 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "utils"
version = "0.1.0"
version = "0.10.0"
dependencies = [
"clap",
"libc",
"log",
"s390_pv",
"serde",
]
[[package]]
name = "vcpkg"
@@ -1219,15 +714,6 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "want"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e"
dependencies = [
"try-lock",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
@@ -1277,7 +763,16 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
"windows-targets 0.48.0",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.4",
]
[[package]]
@@ -1295,6 +790,21 @@ dependencies = [
"windows_x86_64_msvc 0.48.0",
]
[[package]]
name = "windows-targets"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b"
dependencies = [
"windows_aarch64_gnullvm 0.52.4",
"windows_aarch64_msvc 0.52.4",
"windows_i686_gnu 0.52.4",
"windows_i686_msvc 0.52.4",
"windows_x86_64_gnu 0.52.4",
"windows_x86_64_gnullvm 0.52.4",
"windows_x86_64_msvc 0.52.4",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
@@ -1307,6 +817,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
@@ -1319,6 +835,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
@@ -1331,6 +853,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
[[package]]
name = "windows_i686_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
@@ -1343,6 +871,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
[[package]]
name = "windows_i686_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
@@ -1355,6 +889,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
@@ -1367,6 +907,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
@@ -1379,6 +925,12 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"
[[package]]
name = "zerocopy"
version = "0.7.32"

View File

@@ -3,6 +3,7 @@ members = [
"pv",
"pv_core",
"pvapconfig",
"pvattest",
"pvsecret",
"utils",
]

View File

@@ -19,7 +19,7 @@ ifneq (${HAVE_CARGO},0)
ifneq (${HAVE_OPENSSL},0)
ifneq (${HAVE_LIBCURL},0)
PV_TARGETS := pvsecret pvapconfig
PV_TARGETS := pvsecret pvapconfig pvattest
PV_BUILD_TARGETS := $(PV_TARGETS)
CARGO_TEST_TARGETS += $(addsuffix .test,pv $(PV_TARGETS))
@@ -58,6 +58,8 @@ skip-pv-build:
all: $(BUILD_TARGETS)
install: $(INSTALL_TARGETS)
$(INSTALL) -d -m 755 $(DESTDIR)$(USRBINDIR)
$(INSTALL) -g $(GROUP) -o $(OWNER) -m 755 pvattest/tools/pvextract-hdr "$(DESTDIR)$(USRBINDIR)"
print-rust-targets:
echo $(BUILD_TARGETS)
@@ -76,6 +78,7 @@ install-rust-tools: $(BUILD_TARGETS)
$(INSTALL) target/release/$(target) $(DESTDIR)$(USRBINDIR);)
install-man:
$(INSTALL) -d -m 755 $(DESTDIR)$(MANDIR)/man1
$(foreach target,$(CARGO_TARGETS),\
$(INSTALL) -m 644 $(target)/man/*.1 -t $(DESTDIR)$(MANDIR)/man1;)
$(foreach target,$(PV_TARGETS),\

View File

@@ -37,6 +37,7 @@ Tip: You can use `make version` to get the version string.
## Tools
* __pvsecret__ _Manage secrets for IBM Secure Execution guests_
* __pvapconfig__ _automatic configure APQNs within an SE KVM guest_
## Writing new tools
We encourage to use Rust for new tools. However, for some use cases it makes

View File

@@ -1,24 +1,26 @@
[package]
name = "pv"
version = "1.0.0"
name = "s390_pv"
version = "0.10.0"
edition.workspace = true
license.workspace = true
description = "s390-tools IBM Secure Execution utilities"
keywords = ["s390", "s390x", "IBM_Secure_Execution"]
repository = "https://github.com/ibm-s390-linux/s390-tools/tree/master/rust"
categories = ["hardware-support"]
readme = "README.md"
[dependencies]
byteorder = "1.3"
clap = { version ="4", features = ["derive", "wrap_help"] }
curl = "0.4.7"
curl = "0.4.44"
foreign-types = "0.3.1"
log = { version = "0.4.6", features = ["std", "release_max_level_debug"] }
openssl = "0.10.49"
openssl = "0.10.57"
openssl-sys = "0.9.92"
serde = { version = "1.0.139", features = ["derive"] }
thiserror = "1.0.33"
utils = {path = "../utils"}
zerocopy = { version="0.7", features = ["derive"] }
openssl_extensions = { path = "openssl_extensions" }
pv_core = { path = "../pv_core" }
pv_core = { path = "../pv_core", package = "s390_pv_core", version = "0.10.0" }
[dev-dependencies]
mockito = {version = "1", default-features = false }
once_cell = "1.19"
serde_test = "1"
serde_test = "1.0.139"

25
rust/pv/README.md Normal file
View File

@@ -0,0 +1,25 @@
<!--
SPDX-License-Identifier: MIT
Copyright 2024 IBM Corp.
-->
# s390_pv - library for pv-tools
This library is intended to be used by tools and libraries that
are used for creating and managing [IBM Secure Execution](https://www.ibm.com/docs/en/linux-on-systems?topic=virtualization-secure-execution) guests.
`pv` provides abstraction layers for encryption, secure memory management,
and accessing the uvdevice.
If your project is not targeted to provide tooling for and/or managing of IBM Secure execution
guests, do **not** use this crate.
## OpenSSL 1.1.0+ is required
If you do not need any OpenSSL features use [s390_pv_core](https://crates.io/crates/s390_pv_core).
This crate reexports all symbols from `s390_pv_core`. If your project uses this crate do **not** include `s390_pv_core` as well.
## Import crate
The recommended way of importing this crate is:
```bash
cargo add s390_pv --rename pv
```

View File

@@ -1,12 +0,0 @@
[package]
name = "openssl_extensions"
version = "0.1.0"
edition.workspace = true
license.workspace = true
[dependencies]
foreign-types = "0.3.1"
libc = {version = "0.2.49", features = [ "extra_traits"] }
log = { version = "0.4.6", features = ["std", "release_max_level_debug"] }
openssl = "0.10.49"
openssl-sys = "0.9.85"

View File

@@ -1,45 +0,0 @@
// SPDX-License-Identifier: MIT
//
// Copyright IBM Corp. 2023
#![allow(
clippy::inconsistent_digit_grouping,
clippy::uninlined_format_args,
clippy::unusual_byte_groupings
)]
use std::env;
fn main() {
if let Ok(vars) = env::var("DEP_OPENSSL_CONF") {
for var in vars.split(',') {
println!("cargo:rustc-cfg=osslconf=\"{}\"", var);
}
}
if let Ok(version) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
let version = u64::from_str_radix(&version, 16).unwrap();
if version >= 0x1_00_01_00_0 {
println!("cargo:rustc-cfg=ossl101");
}
if version >= 0x1_00_02_00_0 {
println!("cargo:rustc-cfg=ossl102");
}
if version >= 0x1_01_00_00_0 {
println!("cargo:rustc-cfg=ossl110");
}
if version >= 0x1_01_00_07_0 {
println!("cargo:rustc-cfg=ossl110g");
}
if version >= 0x1_01_00_08_0 {
println!("cargo:rustc-cfg=ossl110h");
}
if version >= 0x1_01_01_00_0 {
println!("cargo:rustc-cfg=ossl111");
}
if version >= 0x3_00_00_00_0 {
println!("cargo:rustc-cfg=ossl300");
}
}
}

View File

@@ -1,26 +0,0 @@
// SPDX-License-Identifier: MIT
//
// Copyright IBM Corp. 2023
#![doc(hidden)]
/// Extensions to the rust-openssl crate, that are not upstream yet
/// Upstreaming mostly work in progress
pub mod akid;
pub mod crl;
mod stackable_crl;
/// Test if two CRLs are equal.
///
/// relates to X509_CRL_match
/// (Upstream is missing that functionality)
pub fn x509_crl_eq(a: &openssl::x509::X509CrlRef, b: &openssl::x509::X509CrlRef) -> bool {
use foreign_types::ForeignTypeRef;
let cmp = unsafe { openssl_sys::X509_CRL_match(a.as_ptr(), b.as_ptr()) };
cmp == 0
}
#[allow(dead_code)]
mod test_utils {
include!("../../src/test_utils.rs");
}

View File

@@ -1 +0,0 @@
../../tests/assets

Some files were not shown because too many files have changed in this diff Show More