Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a C++ function with root privileges, without executing the whole program as root

At the moment I am playing around with bluetooth LE and iBeacon devices. I wrote an Server that keeps looking for nearby beacons.

My server follows this example (Link)

Unfortunately calling the function:

hci_le_set_scan_parameters()

requires root privileges.

As I don't want to run the whole server with root privileges I wanted to ask if there is any possibility for calling only this function with root privileges?

I am aware that asking for sudo when executing a program is always at least questionable I could not find any other possibility to scan for iBeacons. If there is another possibility I am happy to hear about it as well.

Thanks for your help and kind regards

nPLus

like image 647
Nplus Avatar asked Apr 29 '26 08:04

Nplus


1 Answers

According to POSIX, UID/GID are process attributes. All code inside your process is executed with UID/GID currently set for the whole process.

You could start server as root and immediately drop root privileges. Then, you can temporary gain root privileges using seteuid(2) when executing your function.

See also this answer.

You can also gain only selected capabilities(7) instead (temporary or permanently).


Thread-safety note

AFAIK, on Linux UID/GID are per-thread attributes and it's possible to set them for single thread, see NOTES section in seteuid() man-page and this post.

like image 185
gavv Avatar answered May 02 '26 03:05

gavv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!