@@ -96,77 +96,6 @@ pub struct PreparedRuleset {
9696 compatibility : LandlockCompatibility ,
9797}
9898
99- impl PreparedRuleset {
100- /// Try to add a read-write rule for `/dev/tty` (the controlling terminal).
101- ///
102- /// `/dev/tty` is a magic device that refers to the calling process's
103- /// controlling terminal. The supervisor process has no controlling terminal,
104- /// so `PathFd::new("/dev/tty")` fails with `ENXIO` during [`prepare`].
105- /// After the child calls `setsid()` + `TIOCSCTTY`, `/dev/tty` becomes
106- /// accessible. This method lets the child add the rule before
107- /// `restrict_self()`.
108- ///
109- /// Consumes and returns `self` because `RulesetCreated::add_rule` takes
110- /// ownership. Failures are silently ignored — `/dev/tty` is only needed
111- /// by interactive TUI programs (`prompt_toolkit`, ratatui, etc.).
112- pub fn add_dev_tty_if_available ( self ) -> Self {
113- let path = Path :: new ( "/dev/tty" ) ;
114- let Ok ( path_fd) = PathFd :: new ( path) else {
115- return self ; // no controlling terminal — nothing to do
116- } ;
117- let abi = ABI :: V2 ;
118- let Ok ( allowed_access) = access_for_path_fd ( & path_fd, AccessFs :: from_all ( abi) , abi) else {
119- return self ;
120- } ;
121- let compatibility = self . compatibility ;
122- // add_rule takes ownership of `self.ruleset`. On success we get
123- // the updated ruleset back; on error it is consumed irreversibly.
124- // The error requires a landlock_add_rule syscall failure (kernel
125- // bug), so we treat it as unreachable and fall through without
126- // the /dev/tty rule.
127- match self
128- . ruleset
129- . add_rule ( PathBeneath :: new ( path_fd, allowed_access) )
130- {
131- Ok ( ruleset) => Self {
132- ruleset,
133- compatibility,
134- } ,
135- Err ( err) => {
136- tracing:: debug!(
137- error = %err,
138- "Landlock add_rule for /dev/tty failed (non-fatal)"
139- ) ;
140- // Ruleset is consumed; build a fresh empty one so
141- // enforce() can still call restrict_self(). The original
142- // rules were already committed to the kernel fd, but that
143- // fd is now gone, so enforce will apply a maximally
144- // restrictive empty ruleset. This is preferable to
145- // skipping Landlock entirely.
146- let fallback = Ruleset :: default ( )
147- . set_compatibility ( compat_level ( & compatibility) )
148- . handle_access ( AccessFs :: from_all ( abi) )
149- . and_then ( Ruleset :: create) ;
150- fallback. map_or_else (
151- |_| {
152- // Cannot recover at all. The process will
153- // continue without Landlock enforcement.
154- // Return a dummy that enforce() can handle.
155- unreachable ! (
156- "failed to create fallback Landlock ruleset \
157- after add_rule failure for /dev/tty"
158- )
159- } ,
160- |ruleset| Self {
161- ruleset,
162- compatibility,
163- } ,
164- )
165- }
166- }
167- }
168- }
169-
17099#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
171100enum PathOpenMode {
172101 Privileged ,
0 commit comments