-
Notifications
You must be signed in to change notification settings - Fork 15
feat(cardwired): add a SmartPolicy interface #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
10e3217
feat(cardwired): add a new smart policy interface
luytan bb1ffc6
chore(cardwired): clean up smart api code
luytan 9bde3d3
fix(cardwired): handle aya map error
luytan 57046d0
fix(cardwired): remove PID from the contrary map before inserting
luytan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| use aya::maps::{HashMap as AyaHashMap, MapError as AyaMapError}; | ||
| use cardwire_ebpf_userspace::EbpfBlocker; | ||
| use std::{path::Path, sync::Arc}; | ||
|
|
||
| use tokio::sync::RwLock; | ||
| use zbus::{ | ||
| fdo::{self, Error::Failed}, interface | ||
| }; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct SmartPolicyInterface { | ||
| pid_map: Arc<RwLock<AyaHashMap<aya::maps::MapData, u32, u32>>>, | ||
| forced_map: Arc<RwLock<AyaHashMap<aya::maps::MapData, u32, u32>>>, | ||
| } | ||
|
|
||
| impl SmartPolicyInterface { | ||
| pub fn build(blocker: &mut EbpfBlocker) -> Self { | ||
| let pid_map = Arc::clone(&blocker.pid_map); | ||
| let forced_map = Arc::clone(&blocker.forced_map); | ||
|
|
||
| Self { | ||
| pid_map, | ||
| forced_map, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[interface(name = "org.opengamingcollective.cardwire.SmartPolicy")] | ||
| impl SmartPolicyInterface { | ||
| /// Authorized a pid to access a specific GPU | ||
| pub async fn request_process_access( | ||
| &self, | ||
| pid: u32, | ||
| policy: String, | ||
| value: u32, | ||
| ) -> Result<(), fdo::Error> { | ||
| // Check if the process exists, leave if it doesnt | ||
| if !Path::new(&format!("/proc/{}", pid)).exists() { | ||
| return Err(fdo::Error::Failed("process doesn't exist".to_string())); | ||
| } | ||
| // Match the policy and add the pid to the corresponding ebpf map | ||
| match policy.as_str() { | ||
| // Default, do nothing | ||
| "Default" => Ok(()), | ||
| // Equivalent to CARDWIRE_ALLOW=1, show both iGPU and dGPU | ||
| "Allow_dGPU" => { | ||
| { | ||
| // First remove the PID from the other map if present | ||
| let mut forced_map = self.forced_map.write().await; | ||
| forced_map | ||
| .remove(&pid) | ||
| .map_err(|err| fdo::Error::Failed(err.to_string()))?; | ||
| } | ||
| let mut pid_map = self.pid_map.write().await; | ||
| pid_map | ||
| .insert(pid, 0, 0) | ||
| .map_err(|err| fdo::Error::Failed(err.to_string())) | ||
| } | ||
| // Equivalent to CARDWIRE_FORCE_DGPU=value | ||
| "Force_dGPU" => { | ||
| { | ||
| // First remove the PID from the other map if present | ||
| let mut pid_map = self.pid_map.write().await; | ||
| pid_map | ||
| .remove(&pid) | ||
| .map_err(|err| fdo::Error::Failed(err.to_string()))?; | ||
| } | ||
| let mut force_map = self.forced_map.write().await; | ||
| force_map | ||
| .insert(pid, value, 0) | ||
| .map_err(|err| Failed(err.to_string())) | ||
| } | ||
| // Equivalent to CARDWIRE_FORCE_GPU=value | ||
| "Force_GPU" => { | ||
| { | ||
| // First remove the PID from the other map if present | ||
| let mut pid_map = self.pid_map.write().await; | ||
| pid_map | ||
| .remove(&pid) | ||
| .map_err(|err| fdo::Error::Failed(err.to_string()))?; | ||
| } | ||
| let mut force_map = self.forced_map.write().await; | ||
| force_map | ||
| .insert(pid, value, 0) | ||
| .map_err(|err| Failed(err.to_string())) | ||
| } | ||
|
luytan marked this conversation as resolved.
|
||
| _ => Err(fdo::Error::InvalidArgs(format!("invalid arg: {}", policy))), | ||
| } | ||
| } | ||
|
|
||
| /// Check if the process is inside PID or FORCED map, and return the map type with the gpu_id | ||
| /// associed | ||
| pub async fn get_process_status(&self, pid: u32) -> Result<(String, Option<u32>), fdo::Error> { | ||
| let mut status = String::new(); | ||
| let mut gpu_id: Option<u32> = None; | ||
|
|
||
| { | ||
| let pid_map = self.pid_map.read().await; | ||
| match pid_map.get(&pid, 0) { | ||
| Ok(id) => { | ||
| status = "Allowed".to_string(); | ||
| gpu_id = Some(id) | ||
| } | ||
| Err(err) => match err { | ||
| AyaMapError::KeyNotFound => {} | ||
| _ => { | ||
| return Err(fdo::Error::Failed(format!( | ||
| "Couldn't read PID MAP: {}", | ||
| err | ||
| ))); | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| { | ||
| let forced_map = self.forced_map.read().await; | ||
| match forced_map.get(&pid, 0) { | ||
| Ok(id) => { | ||
| status = "Forced".to_string(); | ||
| gpu_id = Some(id) | ||
| } | ||
| Err(err) => match err { | ||
| AyaMapError::KeyNotFound => {} | ||
| _ => { | ||
| return Err(fdo::Error::Failed(format!( | ||
| "Couldn't read FORCED MAP: {}", | ||
| err | ||
| ))); | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| Ok((status, gpu_id)) | ||
|
luytan marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
Bind the policy to a process instance, not only a PID.
By the process-binding spell,
/proc/<pid>existence is a TOCTOU check. The process can exit after the check and before the map insertion. If exit cleanup occurs before insertion, the stale entry can apply to a later process that reuses the PID.Store and validate a process-instance identity during kernel enforcement. Do not treat a PID-only map entry as a stable authorization target.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will be fixed in another PR, make a issue for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 2088
@luytan, the wizard has opened and assigned follow-up issue#156: Bind Smart Policy entries to process instances to prevent PID reuse.It includes the required changes, acceptance criteria, and backlinks to PR
#155and this review discussion.You are interacting with an AI system.