Files
GlobalProtect-openconnect/crates/gpapi/src/process/desktop_launcher.rs
T
2026-10-12 00:26:32 +08:00

239 lines
8.2 KiB
Rust

use std::{
collections::HashMap,
fs::{File, OpenOptions},
path::Path,
process::{ExitStatus, Stdio},
sync::Arc,
};
use super::rendering_recovery::RecoveryState;
use crate::service::transport::{DESKTOP_GUI_EXITED, DESKTOP_RENDERING_RETRY};
use anyhow::{Context, ensure};
use common::binary_paths;
use log::warn;
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWriteExt},
process::Command,
};
use tokio_util::sync::CancellationToken;
use zeroize::Zeroizing;
use super::{CommandExt, gui_launcher::GuiLauncher};
use crate::service::transport::{MAX_CREDENTIAL_FRAME, SessionCredential};
/// Owns the desktop GUI and its privileged service without leaving the desktop session.
pub struct DesktopLauncher<'a> {
version: &'a str,
minimized: bool,
log_file: Option<String>,
}
impl<'a> DesktopLauncher<'a> {
pub fn new(version: &'a str) -> Self {
Self {
version,
minimized: false,
log_file: None,
}
}
pub fn minimized(mut self, minimized: bool) -> Self {
self.minimized = minimized;
self
}
pub fn log_file(mut self, log_file: &str) -> Self {
self.log_file = Some(log_file.to_owned());
self
}
pub async fn launch(&self) -> anyhow::Result<ExitStatus> {
ensure!(
uzers::get_effective_uid() != 0,
"The desktop launcher must not run as root"
);
let mut command = Command::new_pkexec(binary_paths::gpservice());
command
.arg("--desktop-credentials")
.stdin(Stdio::piped())
.stdout(Stdio::piped());
if let Some(path) = &self.log_file {
command.stderr(Stdio::from(create_log_file(Path::new(path))?));
}
let mut service = command.spawn().context("Failed to start the privileged service")?;
let mut input = service.stdin.take().context("Service input pipe is unavailable")?;
let mut output = service
.stdout
.take()
.context("Service credential pipe is unavailable")?;
let mut received_credential = false;
let mut cancelled = false;
let cancellation = CancellationToken::new();
let mut recovery = RecoveryState::default();
let result = {
let operation = async {
let mut minimized = self.minimized;
let mut expecting_renewal = false;
let mut program = None;
loop {
let credential = tokio::select! {
biased;
_ = cancellation.cancelled() => return Ok(()),
result = read_credential(&mut output) => result?,
};
let Some(credential) = credential else {
ensure!(
!expecting_renewal,
"Service closed before issuing the recovery credential"
);
return Ok(());
};
expecting_renewal = false;
received_credential = true;
ensure!(
credential.product_version() == self.version,
"Service product version mismatch"
);
let mut envs: HashMap<String, String> = std::env::vars().collect();
if recovery.dmabuf_disabled() {
envs.insert("WEBKIT_DISABLE_DMABUF_RENDERER".into(), "1".into());
}
if let Some(path) = &self.log_file {
envs.insert("GP_LOG_FILE".into(), path.clone());
}
let launcher = GuiLauncher::new(self.version, Arc::new(credential))
.envs(envs)
.minimized(minimized);
let mut unexpected = [0];
// Program resolution may launch the WebKit download helper. Keep it under
// the same cancellation, service ownership and recovery budget as gpgui.
let attempt = async {
if program.is_none() {
program = Some(launcher.resolve_program(&cancellation).await?);
}
launcher.launch_program(program.as_ref().unwrap(), &cancellation).await
};
tokio::pin!(attempt);
let outcome = tokio::select! {
biased;
_ = cancellation.cancelled() => { let _ = attempt.await; return Ok(()); }
result = &mut attempt => result,
status = service.wait() => {
cancellation.cancel(); let _ = attempt.await;
anyhow::bail!("The privileged service exited while the GUI was running: {}", status?);
}
result = output.read(&mut unexpected) => {
cancellation.cancel(); let _ = attempt.await;
ensure!(result? == 0, "Unexpected service credential data while the GUI is running");
anyhow::bail!("The privileged service closed its credential channel");
}
};
if cancellation.is_cancelled() {
return Ok(());
}
match outcome {
Err(error) if recovery.recover(&error) => {
warn!("{error}; restarting the desktop launch with DMABUF disabled and opaque windows");
expecting_renewal = true;
input.write_all(&[DESKTOP_RENDERING_RETRY]).await?;
}
Err(error) => return Err(recovery.terminal(error)),
Ok(_) => {
input.write_all(&[DESKTOP_GUI_EXITED]).await?;
minimized = false;
}
}
}
};
tokio::pin!(operation);
let result = tokio::select! {
biased;
_ = crate::utils::shutdown_signal() => {
cancelled = true;
cancellation.cancel();
operation.await
},
result = &mut operation => result,
};
result
};
let result = recovery.finish(result);
// EOF lets the service revoke the credential and disconnect the VPN gracefully.
drop(input);
if cancelled && !received_credential {
// pkexec is still user-owned during authorization. After elevation, EOF owns shutdown.
if let Err(error) = service.start_kill()
&& error.kind() != std::io::ErrorKind::PermissionDenied
{
return Err(error.into());
}
}
let status = service.wait().await?;
result?;
ensure!(
cancelled || status.success(),
"The privileged service exited with {status}"
);
Ok(status)
}
}
fn create_log_file(path: &Path) -> std::io::Result<File> {
// Start a fresh desktop session, then let both processes append without overwriting each other.
File::create(path)?;
OpenOptions::new().append(true).open(path)
}
async fn read_credential(reader: &mut (impl AsyncRead + Unpin)) -> anyhow::Result<Option<SessionCredential>> {
let mut header = [0; 2];
if reader.read(&mut header[..1]).await? == 0 {
return Ok(None);
}
reader.read_exact(&mut header[1..]).await?;
let length = usize::from(u16::from_be_bytes(header));
ensure!(
length != 0 && length + 2 <= MAX_CREDENTIAL_FRAME,
"Invalid service credential length"
);
let mut frame = Zeroizing::new(vec![0; length + 2]);
frame[..2].copy_from_slice(&header);
reader.read_exact(&mut frame[2..]).await?;
Ok(Some(SessionCredential::decode_frame(&frame)?))
}
#[cfg(test)]
mod tests {
use std::io::Write;
use super::*;
#[test]
fn desktop_log_starts_fresh_and_service_writes_preserve_gui_logs() {
let dir = tempfile::TempDir::new().unwrap();
let path = dir.path().join("desktop.log");
std::fs::write(&path, "previous session\n").unwrap();
let mut service = create_log_file(&path).unwrap();
writeln!(service, "[gpservice] started").unwrap();
let mut gui = OpenOptions::new().append(true).open(&path).unwrap();
writeln!(gui, "[gpgui] initialized").unwrap();
writeln!(service, "[gpservice] connected").unwrap();
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
"[gpservice] started\n[gpgui] initialized\n[gpservice] connected\n"
);
}
#[tokio::test]
async fn credential_channel_distinguishes_clean_close_from_truncated_or_oversized_frames() {
assert!(read_credential(&mut &[][..]).await.unwrap().is_none());
assert!(read_credential(&mut &[0][..]).await.is_err());
assert!(read_credential(&mut &[0, 0][..]).await.is_err());
assert!(read_credential(&mut &[255, 255][..]).await.is_err());
let credential = SessionCredential::generate(uuid::Uuid::new_v4(), "test").unwrap();
let frame = credential.encode_frame().unwrap();
let decoded = read_credential(&mut frame.as_slice()).await.unwrap().unwrap();
assert_eq!(decoded.session_id(), credential.session_id());
}
}