Skip to content

Commit 32c0c2b

Browse files
ppennaCopilot
andcommitted
fuse: fail FUSE_INIT when init wants flags the client did not offer
Session::dispatch_init asserted that the flags which Fuse::init wants are among those that the client offered (microsoft/nvx#1630). A file system whose init wants a flag that the guest did not offer thus panicked, which aborts the VMM process, since its profiles abort on panic, instead of failing FUSE_INIT. And dispatch_init read the client's capabilities back from the SessionInfo that init may replace, as with the public SessionInfo::from(SessionInfoState): an init that claimed every capability passed the check, sent the guest flags that it never offered, and left in save_state a version and capabilities that the client never sent. dispatch_init now keeps the version and the client's capabilities that it sets before init, and restores them after it, whatever init did to the SessionInfo. When init wants a flag that the client did not offer, or an extended flag that it did not offer while init keeps FUSE_INIT_EXT, without which dispatch_init discards the extended flags, dispatch_init logs a rate-limited error, resets the SessionInfo, so that the session stays uninitialized and saves no negotiation, and replies with EIO, as for its other FUSE_INIT failures. Fuse::init documents both. VirtioFs::init wants only flags that the client offers, so its negotiation does not change. Tests, in fuse's session tests of init: - init_rejects_flags_the_client_did_not_offer, with a file system that wants a flag or an extended flag that the client did not offer, panics at feature/microvm/dev 51e9af7 on the assertion of each. - init_keeps_the_client_version_and_capabilities, with one whose init claims version 8.0 and every capability, there replies without an error to a want beyond the offer, and saves that version and those capabilities. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 51e9af7 commit 32c0c2b

3 files changed

Lines changed: 137 additions & 2 deletions

File tree

‎vm/devices/support/fs/fuse/src/lib.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ pub trait Fuse {
257257
}
258258

259259
/// Negotiate file system parameters with the client.
260+
///
261+
/// `info.want` must hold only flags that the client offers in `info.capable()`, and
262+
/// `info.want2` only extended flags that it offers in `info.capable2()`, unless `info.want`
263+
/// drops `FUSE_INIT_EXT`, which discards `info.want2`. Otherwise, the session fails
264+
/// `FUSE_INIT` with `EIO` and stays uninitialized. The session keeps the client's version and
265+
/// capabilities, even if `init` replaces `info`.
260266
fn init(&self, _info: &mut SessionInfo) {}
261267

262268
/// Opens a directory.

‎vm/devices/support/fs/fuse/src/session.rs‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,38 @@ impl Session {
485485
}
486486
info.time_gran = 1;
487487
info.max_write = DEFAULT_MAX_PAGES * PAGE_SIZE;
488+
let SessionInfo {
489+
major,
490+
minor,
491+
capable,
492+
capable2,
493+
..
494+
} = *info;
488495
self.fs.init(&mut info);
496+
// The version and the client's capabilities belong to the session:
497+
// keep them, even if `init` replaced the whole `info`.
498+
info.major = major;
499+
info.minor = minor;
500+
info.capable = capable;
501+
info.capable2 = capable2;
489502

490-
assert!(info.want & !info.capable == 0);
491503
// If the filesystem cleared FUSE_INIT_EXT from want, force want2 to
492504
// zero so we never reply with flags2 the kernel won't expect.
493505
if info.want & FUSE_INIT_EXT == 0 {
494506
info.want2 = 0;
495507
}
496-
assert!(info.want2 & !info.capable2 == 0);
508+
let unoffered = info.want & !info.capable;
509+
let unoffered2 = info.want2 & !info.capable2;
510+
if unoffered != 0 || unoffered2 != 0 {
511+
tracelimit::error_ratelimited!(
512+
unoffered = format_args!("{unoffered:#x}"),
513+
unoffered2 = format_args!("{unoffered2:#x}"),
514+
"File system wants FUSE flags that the client did not offer",
515+
);
516+
// Leave the session uninitialized, without a negotiation.
517+
*info = SessionInfo::default();
518+
return Err(lx::Error::EIO.into());
519+
}
497520

498521
// Report the negotiated values back to the client.
499522
// TODO: Set map_alignment for DAX.
@@ -670,6 +693,7 @@ enum OperationError {
670693
mod tests {
671694
mod error_reply;
672695
mod harness;
696+
mod init;
673697
mod ioctl;
674698
mod max_size;
675699
mod reply_len;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
//! Tests of the flags that a file system's `init` negotiates in `FUSE_INIT`.
5+
6+
use super::harness::dispatch_request;
7+
use super::*;
8+
use crate::session::saved_state::SessionInfoState;
9+
use crate::session::saved_state::SessionState;
10+
use test_with_tracing::test;
11+
12+
/// A file system whose `init` wants flags whether or not the client offered
13+
/// them.
14+
struct UnconditionalWantFs {
15+
want: u32,
16+
want2: u32,
17+
}
18+
19+
impl Fuse for UnconditionalWantFs {
20+
fn init(&self, info: &mut SessionInfo) {
21+
info.want |= self.want;
22+
info.want2 |= self.want2;
23+
}
24+
}
25+
26+
/// A file system that wants a flag or an extended flag that the client did
27+
/// not offer fails `FUSE_INIT` with `EIO`, which leaves the session
28+
/// uninitialized, without a negotiation to save.
29+
#[test]
30+
fn init_rejects_flags_the_client_did_not_offer() {
31+
// (flags, flags2) that the client offers, and (want, want2) that the file
32+
// system adds.
33+
let cases = [
34+
((FUSE_ASYNC_READ, 0), (FUSE_DO_READDIRPLUS, 0)),
35+
(
36+
(FUSE_ASYNC_READ | FUSE_INIT_EXT, 0),
37+
(0, FUSE_DIRECT_IO_ALLOW_MMAP_FLAG2),
38+
),
39+
];
40+
for ((flags, flags2), (want, want2)) in cases {
41+
let session = Session::new(UnconditionalWantFs { want, want2 });
42+
let request = make_init_request(7, 39, 131072, flags, flags2);
43+
let (header, reply) = dispatch_request(&session, &request);
44+
assert_eq!(header.error, -lx::EIO);
45+
assert!(reply.is_empty());
46+
assert!(!session.is_initialized());
47+
assert_eq!(session.save_state(), SessionState::default());
48+
}
49+
}
50+
51+
/// A file system whose `init` replaces the session information with one that
52+
/// claims another version and every capability.
53+
struct ForgingFs {
54+
want: u32,
55+
}
56+
57+
impl Fuse for ForgingFs {
58+
fn init(&self, info: &mut SessionInfo) {
59+
*info = SessionInfoState {
60+
major: FUSE_KERNEL_VERSION + 1,
61+
minor: 0,
62+
capable: !0,
63+
capable2: !0,
64+
want: info.want | self.want,
65+
..SessionInfoState::from(*info)
66+
}
67+
.into();
68+
}
69+
}
70+
71+
/// The session keeps the version and the capabilities that the client sent,
72+
/// whatever `init` does to the session information: claiming every capability
73+
/// neither lets the file system want a flag that the client did not offer,
74+
/// nor changes what the session saves.
75+
#[test]
76+
fn init_keeps_the_client_version_and_capabilities() {
77+
let request = make_init_request(7, 31, 131072, FUSE_ASYNC_READ, 0);
78+
79+
let session = Session::new(ForgingFs {
80+
want: FUSE_DO_READDIRPLUS,
81+
});
82+
let (header, reply) = dispatch_request(&session, &request);
83+
assert_eq!(header.error, -lx::EIO);
84+
assert!(reply.is_empty());
85+
assert!(!session.is_initialized());
86+
assert_eq!(session.save_state(), SessionState::default());
87+
88+
let session = Session::new(ForgingFs { want: 0 });
89+
let (header, reply) = dispatch_request(&session, &request);
90+
assert_eq!(header.error, 0);
91+
let (out, _) = fuse_init_out::read_from_prefix(&reply).unwrap();
92+
assert_eq!(out.flags, FUSE_ASYNC_READ);
93+
let saved = session.save_state();
94+
assert!(saved.initialized);
95+
assert_eq!(
96+
(
97+
saved.info.major,
98+
saved.info.minor,
99+
saved.info.capable,
100+
saved.info.capable2
101+
),
102+
(FUSE_KERNEL_VERSION, 31, FUSE_ASYNC_READ, 0)
103+
);
104+
saved.validate().unwrap();
105+
}

0 commit comments

Comments
 (0)