Skip to content

Commit a1cbb34

Browse files
committed
Enforce root permission on AccessPoint constructor
1 parent 7f07f33 commit a1cbb34

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

Cargo.lock

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proton_err/src/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub enum ProtonError {
3939
/// Could not parse into CIDR range.
4040
CouldNotParseAsCidr (String),
4141

42+
/// Root permissions required.
43+
MustHaveRootPermissions,
44+
4245
/// CIDR range must contain network gateway.
4346
CidrMustContainGateway {
4447
/// Provided CIDR network range.
@@ -57,6 +60,7 @@ impl Display for ProtonError {
5760
use ProtonError::*;
5861
let error = match self {
5962
MustBeEthernetInterface => "must be Ethernet interface",
63+
MustHaveRootPermissions => "must execute with root permissions",
6064
HotspotNotInitialized => "hotspot not initialized",
6165
CouldNotFindWirelessInterface => "could not find wireless interface",
6266
CouldNotGetDeviceInformation => "could not get wireless device information",

proton_wap/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ name = "proton_wap"
88
path = "src/lib.rs"
99

1010
[dependencies]
11+
nix = { version = "0.29.0", features = ["user"] }
1112

1213
[dependencies.network-manager]
1314
path = "../network-manager"

proton_wap/src/ap.rs

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use network_manager::{
88
ConnectionState,
99
};
1010

11+
use nix::unistd::Uid;
12+
1113
use proton_cfg::HotspotConfig;
1214

1315
use proton_dev::{
@@ -21,6 +23,14 @@ use proton_err::{
2123
};
2224

2325
/// A wireless access point.
26+
///
27+
/// **Note**: to construct and use this, you must run the associated
28+
/// binary with root permissions. This is because some of the functionality
29+
/// of the `AccessPoint` structure requires direct control over your
30+
/// device's network interface.
31+
///
32+
/// This is enforced by `AccessPoint::new()`, as the constructor will return
33+
/// a `ProtonError` if you attempt to execute it without root permission.
2434
pub struct AccessPoint {
2535
/// Device discovery manager.
2636
device_manager: DeviceManager,
@@ -44,6 +54,11 @@ impl AccessPoint {
4454
wlifname: &str,
4555
config: HotspotConfig,
4656
) -> ProtonResult<Self> {
57+
// Check if the user is `root`
58+
if !Uid::effective().is_root() {
59+
return Err (ProtonError::MustHaveRootPermissions);
60+
}
61+
4762
// Initialize NetworkManager API
4863
let network_manager = NetworkManager::new();
4964

0 commit comments

Comments
 (0)