fleet/orbit/pkg/execuser/execuser_linux.go

255 lines
7.7 KiB
Go
Raw Normal View History

package execuser
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
userpkg "github.com/fleetdm/fleet/v4/orbit/pkg/user"
"github.com/rs/zerolog/log"
)
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
// base command to setup an exec.Cmd using `runuser`
func baserun(path string, opts eopts) (cmd *exec.Cmd, err error) {
args, env, userContext, err := getConfigForCommand(path)
2024-11-20 16:44:40 +00:00
if err != nil {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
return nil, fmt.Errorf("get args: %w", err)
2024-11-20 16:44:40 +00:00
}
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
env = append(env,
2024-11-20 16:44:40 +00:00
// Append the packaged libayatana-appindicator3 libraries path to LD_LIBRARY_PATH.
//
// Fleet Desktop doesn't use libayatana-appindicator3 since 1.18.3, but we need to
// keep this to support older versions of Fleet Desktop.
fmt.Sprintf("LD_LIBRARY_PATH=%s:%s", filepath.Dir(path), os.ExpandEnv("$LD_LIBRARY_PATH")),
)
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
for _, nv := range opts.env {
env = append(env, fmt.Sprintf("%s=%s", nv[0], nv[1]))
}
// Hold any command line arguments to pass to the command.
cmdArgs := make([]string, 0, len(opts.args)*2)
if len(opts.args) > 0 {
for _, arg := range opts.args {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
cmdArgs = append(cmdArgs, arg[0])
if arg[1] != "" {
cmdArgs = append(cmdArgs, arg[1])
}
}
}
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
// If the user has an SELinux context, we run the command with runcon so that it has the correct context.
// Otherwise, we run the command directly.
if userContext != nil {
args = append(args, "-c", fmt.Sprintf("runcon \"%s\" env %s %s %s", *userContext, strings.Join(env, " "), path, strings.Join(cmdArgs, " ")))
} else {
args = append(args, "-c", fmt.Sprintf("%s %s %s", strings.Join(env, " "), path, strings.Join(cmdArgs, " ")))
}
// Use runuser to run the command as the login user.
args = append([]string{"runuser"}, args...)
// If a timeout is set, prefix the command with "timeout".
if opts.timeout > 0 {
args = append([]string{"timeout", fmt.Sprintf("%ds", int(opts.timeout.Seconds()))}, args...)
}
cmd = exec.Command(args[0], args[1:]...) // #nosec G204
return
}
// run a command, passing its output to stdout and stderr.
func run(path string, opts eopts) (lastLogs string, err error) {
cmd, err := baserun(path, opts)
if err != nil {
return "", err
}
2024-11-20 16:44:40 +00:00
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
log.Info().Str("cmd", cmd.String()).Msg("running command")
2024-11-20 16:44:40 +00:00
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("open path %q: %w", path, err)
}
return "", nil
}
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
// run a command and return its output and exit code.
2024-11-20 16:44:40 +00:00
func runWithOutput(path string, opts eopts) (output []byte, exitCode int, err error) {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
cmd, err := baserun(path, opts)
2024-11-20 16:44:40 +00:00
if err != nil {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
return nil, -1, err
}
2024-11-20 16:44:40 +00:00
output, err = cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode = exitErr.ExitCode()
return output, exitCode, fmt.Errorf("%q exited with code %d: %w", path, exitCode, err)
}
return output, -1, fmt.Errorf("%q error: %w", path, err)
}
return output, exitCode, nil
}
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
// run a command that requires stdin input, returning a pipe to write to stdin.
func runWithStdin(path string, opts eopts) (io.WriteCloser, error) {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
cmd, err := baserun(path, opts)
if err != nil {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
return nil, err
}
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("stdin pipe: %w", err)
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("open path %q: %w", path, err)
}
return stdin, nil
}
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
func getConfigForCommand(path string) (args []string, env []string, userContext *string, err error) {
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
user, err := userpkg.GetLoginUser()
if err != nil {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
return nil, nil, nil, fmt.Errorf("get user: %w", err)
}
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
log.Info().Str("user", user.Name).Int64("id", user.ID).Msg("attempting to get user session type and display")
// Get user's display session type (x11 vs. wayland).
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
uid := strconv.FormatInt(user.ID, 10)
userDisplaySessionType, err := userpkg.GetUserDisplaySessionType(uid)
if userDisplaySessionType == userpkg.GuiSessionTypeTty {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
return nil, nil, nil, fmt.Errorf("user %q (%d) is not running a GUI session", user.Name, user.ID)
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
}
if err != nil {
// Wayland is the default for most distributions, thus we assume
// wayland if we couldn't determine the session type.
log.Error().Err(err).Msg("assuming wayland session")
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
userDisplaySessionType = userpkg.GuiSessionTypeWayland
}
var display string
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
if userDisplaySessionType == userpkg.GuiSessionTypeX11 {
x11Display, err := getUserX11Display(user.Name)
if err != nil {
log.Error().Err(err).Msg("failed to get X11 display, using default :0")
// TODO(lucas): Revisit when working on multi-user/multi-session support.
// Default to display ':0' if user display could not be found.
// This assumes there's only one desktop session and belongs to the
// user returned in `getLoginUID'.
display = ":0"
} else {
display = x11Display
}
} else {
waylandDisplay, err := getUserWaylandDisplay(uid)
if err != nil {
log.Error().Err(err).Msg("failed to get wayland display, using default wayland-0")
// TODO(lucas): Revisit when working on multi-user/multi-session support.
// Default to display 'wayland-0' if user display could not be found.
// This assumes there's only one desktop session and belongs to the
// user returned in `getLoginUID'.
display = "wayland-0"
} else {
display = waylandDisplay
}
}
log.Info().
Str("path", path).
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
Str("user", user.Name).
Int64("id", user.ID).
Str("display", display).
Str("session_type", userDisplaySessionType.String()).
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
Msg("running runuser")
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
args = []string{"-l", user.Name}
env = make([]string, 0)
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
if userDisplaySessionType == userpkg.GuiSessionTypeWayland {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
env = append(env, "WAYLAND_DISPLAY="+display)
// For xdg-open to work on a Wayland session we still need to set the DISPLAY variable.
x11Display := ":" + strings.TrimPrefix(display, "wayland-")
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
env = append(env, "DISPLAY="+x11Display)
} else {
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
env = append(env, "DISPLAY="+display)
}
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
env = append(env,
// DBUS_SESSION_BUS_ADDRESS sets the location of the user login session bus.
// Required by the libayatana-appindicator3 library to display a tray icon
// on the desktop session.
//
// This is required for Ubuntu 18, and not required for Ubuntu 21/22
// (because it's already part of the user).
Don't attempt to start fleet desktop if no user is logged in on Linux (#30261) for #29942 # Details This PR addresses an issue on Linux where Orbit repeatedly attempts to launch Fleet Desktop even though no GUI user is logged in. The fix is similar to one implemented for MacOS, where we have Orbit check for the presence of a real user (not a system user like `gdm` or `root`) before trying to launch the desktop app. Part of this work involved moving some functionality from the `execuser` package to the `user` package, to avoid duplicating functionality. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. The changed code is only executed on Linux, so I tested on Ubuntu, Fedora and Debian. Also verified that it still works on MacOS and Windows. --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
2025-06-27 13:41:13 +00:00
fmt.Sprintf("DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%d/bus", user.ID),
)
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
// Get the user's SELinux context (if any).
userContext = userpkg.GetSELinuxUserContext(user)
Run fleet desktop with user SELinux context when applicable (#30882) For #29793 # Details This PR changes the way that Orbit launches processes such as the desktop app on Linux, in order to ensure that on SELinux-enabled systems the correct user context is set when running the command. Previously, `sudo -u` was used to launch commands on Linux. This PR switches to use `runuser` instead, which is recommended in situations where the root user wants to execute a command as a user with reduced privileges (see [the blog post by one of the creators of runuser](https://danwalsh.livejournal.com/55588.html)). This avoids certain errors that can come from interacting with PAM modules as the system user. Additionally, if we detect that SELinux is set up on a system, we now use `runcon` to force the command to run using the logged-in user's SELinux context. It's possible that on some systems they may have configuration where `sudo` will switch to the user's SELinux context automatically, but this is not guaranteed. Using `runuser` + `runcon` is our best bet for ensuring that the desktop app (and anything that it spawns) runs under the correct context. This PR also does some refactoring so that the three `run` methods for Linux (`run`, `runWithOutput` and `runWithStdin`) all use the same base code to create the command with the correct args and env vars, and differ only in how they handle the i/o. # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] ~Manual QA must be performed in the three main OSs, macOS, Windows and Linux.~ (n/a, code is linux only) - [x] ~Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~ n/a # Testing - [x] Ubuntu with SELinux on - [x] Ubuntu with SELinux off - [ ] Fedora with SELinux on - [ ] Fedora with SELinux off - [ ] Debian with SELinux on - [x] Debian with SELinux off - [x] `runWithOutput` still works (tested with `go run ./tools/dialog/main.go --dialog=zenity`) - [ ] ~`runWithStdin` still works~ (this isn't currently used by Linux) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved security and user context handling when launching the fleet desktop application on Linux systems. * **Refactor** * Enhanced process launch mechanism to use proper SELinux context and user session, ensuring processes start under the correct user and security environment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-25 13:44:13 +00:00
return args, env, userContext, nil
}
var whoLineRegexp = regexp.MustCompile(`(\w+)\s+(:\d+)\s+`)
// getUserWaylandDisplay returns the value to set on WAYLAND_DISPLAY for the given user.
func getUserWaylandDisplay(uid string) (string, error) {
matches, err := filepath.Glob("/run/user/" + uid + "/wayland-*")
if err != nil {
return "", fmt.Errorf("list wayland socket files: %w", err)
}
sort.Slice(matches, func(i, j int) bool {
return matches[i] < matches[j]
})
for _, match := range matches {
if strings.HasSuffix(match, ".lock") {
continue
}
return filepath.Base(match), nil
}
return "", errors.New("wayland socket not found")
}
// getUserX11Display returns the value to set on DISPLAY for the given user.
func getUserX11Display(user string) (string, error) {
cmd := exec.Command("who")
var stdout bytes.Buffer
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("run 'who' to get user display: %w", err)
}
return parseWhoOutputForDisplay(&stdout, user)
}
func parseWhoOutputForDisplay(output io.Reader, user string) (string, error) {
scanner := bufio.NewScanner(output)
for scanner.Scan() {
line := scanner.Text()
matches := whoLineRegexp.FindStringSubmatch(line)
if len(matches) > 1 && matches[1] == user {
return matches[2], nil
}
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("scanner error: %w", err)
}
return "", errors.New("display not found on who output")
}