Securing Files
Use libpeios to open a file with explicit access or to read and change its security descriptor. Select the descriptor components needed by the task so that an update preserves unrelated security settings.
Before you start #
Prepare the required authority and inputs:
- A Peios kernel with the Kernel Access Control Subsystem (KACS).
- A process token with the rights needed for the requested operation.
- The libpeios headers and matching library.
- A validated security descriptor for operations that supply one.
Opening a file #
A native file open requests a specific access mask. The resulting handle carries the access granted at open. Choose that mask deliberately before passing the handle to another process.
- Initialize
struct peios_open_paramsto zero. - Set
desired_accessto the file rights the caller needs. - Choose the disposition that matches the operation. Use
KACS_DISPOSITION_CREATEwhen an existing path must be an error, orKACS_DISPOSITION_OPENwhen the file must already exist. - For creation, supply the creator descriptor when required by the
application's security policy. For an existing file, leave
sdasNULLandsd_lenas zero. - Call
peios_file_openand check its return value before using the descriptor or the status output. - Close the returned descriptor when its ownership ends.
If a create-only operation reports EEXIST, decide whether the application
can accept the existing object before opening it separately. A preceding
path-existence check cannot make creation and opening atomic.
Supply a new file's descriptor when you create it, rather than creating the file and then setting its descriptor. Between those two calls the file exists with whatever descriptor it inherited. The handle a creating open returns has the access it asked for, even where the new descriptor would not grant it to the creator, so you can create a file only others can read and still write it through that handle.
To keep other processes from writing or deleting the file while you use it,
set KACS_OPEN_SHARE_ACCESS in flags and name what you allow in
share_access.
See opening a file for the parameter contract and the kernel's supported create options.
Reading a security descriptor #
The KACS_SECINFO_* selection identifies which components to read. A
request for the owner and discretionary access control list (DACL) does
not also request the system access control list (SACL).
- Choose
peios_fd_get_sdwhen the application already owns a suitable handle. Otherwise, usepeios_file_get_sdwith the intended path policy. - Probe the required length with a zero-capacity buffer and check for
failure before converting the result to
size_t. - Allocate the buffer and check that allocation succeeded.
- Repeat the query. If the descriptor grows and the call reports
ERANGE, probe again or stop according to the application's retry policy. - Parse only the bytes returned by a successful query.
- Keep the buffer unchanged while using parsed views, then release it.
A size probe does not reserve the descriptor or prevent another process from changing it. Do not use the probe's length as though it were the length returned by the second call.
Changing selected components #
Use peios_sd_builder to construct the descriptor that carries the
replacement components:
- Create a builder and check that it is not
NULL. - Add the replacement components, such as the DACL.
- Obtain the encoded bytes and check for a retained builder error.
- Call
peios_fd_set_sdorpeios_file_set_sdwith the correspondingKACS_SECINFO_*selection. - Check the result before reporting success, then free the builder.
Selecting only KACS_SECINFO_DACL preserves the owner, group, and SACL.
Changing an owner or integrity label has additional permission checks;
possessing a syntactically valid descriptor does not satisfy those checks.
Checking the result #
Read the selected components again through the intended object handle and
compare them with the requested values. Distinguish a failed update from a
failed verification query, and preserve the relevant errno before cleanup.
The file-security reference describes each library entry point. The kernel file-security guide owns the kernel's access checks and descriptor merge rules.