2024-06-27 05:36:33 +02:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
require "bsd/capsicum"
|
|
|
|
|
|
|
|
path = File.join(Dir.home, "bsdcapsicum.txt")
|
|
|
|
file = File.open(path, File::CREAT | File::TRUNC | File::RDWR)
|
|
|
|
file.sync = true
|
2024-06-27 10:09:13 +02:00
|
|
|
print "[parent] Obtain file descriptor (with all capabilities)", "\n"
|
2024-06-27 05:36:33 +02:00
|
|
|
fork do
|
|
|
|
BSD::Capsicum.set_rights!(file, %i[CAP_READ])
|
2024-06-27 10:09:13 +02:00
|
|
|
print "[subprocess] Reduce capabilities to read", "\n"
|
2024-06-27 05:36:33 +02:00
|
|
|
|
|
|
|
file.gets
|
2024-06-27 10:09:13 +02:00
|
|
|
print "[subprocess] Read OK", "\n"
|
2024-06-27 05:36:33 +02:00
|
|
|
|
|
|
|
begin
|
|
|
|
file.write "foo"
|
|
|
|
rescue Errno::ENOTCAPABLE => ex
|
|
|
|
print "[subprocess] Error: #{ex.message} (#{ex.class})", "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Process.wait
|
|
|
|
file.write "[parent] Hello from #{Process.pid}", "\n"
|
2024-06-27 10:09:13 +02:00
|
|
|
print "[parent] Write OK", "\n"
|
2024-06-27 05:36:33 +02:00
|
|
|
|
|
|
|
##
|
2024-06-27 10:11:19 +02:00
|
|
|
# [parent] Obtain file descriptor (with all capabilities)
|
2024-06-27 10:09:13 +02:00
|
|
|
# [subprocess] Reduce capabilities to read
|
|
|
|
# [subprocess] Read OK
|
|
|
|
# [subprocess] Error: Capabilities insufficient @ io_write - /home/user/bsdcapsicum.txt (Errno::ENOTCAPABLE)
|
|
|
|
# [parent] Write OK
|