Skip to content

Commit c391433

Browse files
authored
Merge pull request #2300 from microsoft/agents/issue-2299-workload-id-range
nvx: check workload IDs against one shared range
2 parents 9368b22 + 9b8bb2b commit c391433

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎nvx/src/guest/managed_agent/launch.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::guest::init::ExecRequest;
44
use crate::guest::parse::parse_decimal;
55
use crate::guest::sys::{Errno, c_string};
66
use crate::protocol::GuestExecPayload;
7+
use crate::protocol::workload::WORKLOAD_IDS;
78

89
use super::AgentConfig;
910
use super::environment;
@@ -115,11 +116,11 @@ fn safe_path(path: &[u8]) -> bool {
115116
.all(|component| !matches!(component, b"" | b"." | b".."))
116117
}
117118

118-
/// Parses one nonzero decimal UID or GID while excluding the unchanged-ID sentinel.
119+
/// Parses one decimal UID or GID in [`WORKLOAD_IDS`].
119120
pub(super) fn parse_id(text: &[u8]) -> Result<u32, Errno> {
120121
parse_decimal(text)
121-
.filter(|&id| id != 0 && id < u64::from(u32::MAX))
122122
.and_then(|id| u32::try_from(id).ok())
123+
.filter(|id| WORKLOAD_IDS.contains(id))
123124
.ok_or(Errno::EINVAL)
124125
}
125126
#[cfg(test)]

‎nvx/src/host/sandbox/mod.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ use crate::error::{Error, Result};
6262
use crate::host::artifacts::require_existing;
6363
#[cfg(feature = "cli")]
6464
use crate::protocol::cmdline;
65+
#[cfg(feature = "cli")]
66+
use crate::protocol::workload::WORKLOAD_IDS;
6567

6668
/// The workload identity by default, `65534:65534`.
6769
#[cfg(feature = "cli")]
@@ -106,7 +108,8 @@ pub(crate) fn parse_named_workload_identity(name: &str, value: &str) -> Result<(
106108
))
107109
}
108110

109-
/// Parses a workload UID or GID, between 1 and 4294967294, and names it `name` in its refusal.
111+
/// Parses a workload UID or GID, between 1 and 4294967294 as [`WORKLOAD_IDS`] holds, and names it
112+
/// `name` in its refusal.
110113
///
111114
/// 4294967295 is refused: as `(uid_t)-1` it reads as "leave unchanged" to `setresuid`/`setresgid`,
112115
/// so accepting it would leave a one-shot sandbox workload running as UID 0 and GID 0.
@@ -116,8 +119,14 @@ pub(crate) fn parse_named_workload_identity(name: &str, value: &str) -> Result<(
116119
#[cfg(feature = "cli")]
117120
pub(crate) fn parse_workload_id(name: impl std::fmt::Display, value: &str) -> Result<u32> {
118121
parse_decimal::<u32>(value)
119-
.filter(|&id| id != 0 && id != u32::MAX)
120-
.ok_or_else(|| refused(format!("{name} must be between 1 and 4294967294")))
122+
.filter(|id| WORKLOAD_IDS.contains(id))
123+
.ok_or_else(|| {
124+
refused(format!(
125+
"{name} must be between {} and {}",
126+
WORKLOAD_IDS.start(),
127+
WORKLOAD_IDS.end()
128+
))
129+
})
121130
}
122131

123132
/// Parses nonempty ASCII decimal digits into a native integer, refusing signs and whitespace.

‎nvx/src/protocol/app.rs‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,22 @@ use crate::guest_restore::GuestRestoreRecord;
2626
feature = "cli",
2727
all(feature = "guest", target_os = "linux", target_arch = "x86_64")
2828
))]
29-
use self::workload::WorkloadIdentity;
29+
use self::workload::{WORKLOAD_IDS, WorkloadIdentity};
3030

3131
/// Shared identity data.
3232
#[cfg(any(
3333
feature = "cli",
3434
all(feature = "guest", target_os = "linux", target_arch = "x86_64")
3535
))]
3636
pub(crate) mod workload {
37-
/// The identity that workloads run as: a non-root user and group ID.
37+
use std::ops::RangeInclusive;
38+
39+
/// The user and group IDs that a workload may run as: not root, and not `-1` as `(uid_t)-1`
40+
/// and `(gid_t)-1`, which `setresuid` and `setresgid` read as "leave unchanged", so that
41+
/// accepting it would leave the workload's user or group root's.
42+
pub(crate) const WORKLOAD_IDS: RangeInclusive<u32> = 1..=u32::MAX - 1;
43+
44+
/// The identity that workloads run as: a user and group ID in [`WORKLOAD_IDS`].
3845
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3946
pub(crate) struct WorkloadIdentity {
4047
/// User ID.
@@ -269,13 +276,13 @@ pub(crate) fn decode_activation_record(
269276
Ok((record, workload))
270277
}
271278

272-
/// Matches the guest's non-root credential range, excluding the unchanged-ID sentinel.
279+
/// Checks that the workload's UID and GID both lie in [`WORKLOAD_IDS`].
273280
#[cfg(any(
274281
feature = "cli",
275282
all(feature = "guest", target_os = "linux", target_arch = "x86_64")
276283
))]
277284
fn check_workload_identity(workload: &WorkloadIdentity) -> Result<(), ProtocolError> {
278-
if matches!(workload.uid, 0 | u32::MAX) || matches!(workload.gid, 0 | u32::MAX) {
285+
if !WORKLOAD_IDS.contains(&workload.uid) || !WORKLOAD_IDS.contains(&workload.gid) {
279286
return Err(violation(
280287
"activation workload requires a non-root UID and GID below u32::MAX",
281288
));

0 commit comments

Comments
 (0)