bsdcapsicum.rb/lib/bsd/capsicum.rb

45 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2024-06-25 08:25:53 +02:00
module BSD
end unless defined?(BSD)
module BSD::Capsicum
2024-06-25 05:08:23 +02:00
require_relative "capsicum/version"
2024-06-25 09:37:54 +02:00
require_relative "capsicum/ffi"
extend self
2024-06-25 03:48:14 +02:00
##
2017-05-24 17:33:39 +02:00
# Check if we're in capability mode.
#
2024-06-25 09:16:17 +02:00
# @see https://man.freebsd.org/cgi/man.cgi?query=cap_getmode&apropos=0&sektion=2&format=html cap_getmode(2)
2024-06-25 03:48:14 +02:00
# @raise [SystemCallError]
# Might raise a subclass of SystemCallError
# @return [Boolean]
2024-06-25 05:07:00 +02:00
# Returns true when the current process is in capability mode
def in_capability_mode?
uintp = Fiddle::Pointer.malloc(Fiddle::SIZEOF_UINT)
2024-06-25 09:37:54 +02:00
FFI.cap_getmode(uintp)
if FFI.cap_getmode(uintp).zero?
uintp[0, Fiddle::SIZEOF_UINT].unpack("i") == [1]
2017-05-24 02:18:47 +02:00
else
raise SystemCallError.new("cap_getmode", Fiddle.last_error)
2017-05-24 02:18:47 +02:00
end
ensure
uintp.call_free
2017-05-24 02:18:47 +02:00
end
alias_method :capability_mode?, :in_capability_mode?
2017-05-24 02:18:47 +02:00
2024-06-25 03:48:14 +02:00
##
2024-06-25 05:52:16 +02:00
# Enter a process into capability mode
2017-05-24 17:33:39 +02:00
#
2024-06-25 09:16:17 +02:00
# @see https://man.freebsd.org/cgi/man.cgi?query=cap_enter&apropos=0&sektion=2&format=html cap_enter(2)
2024-06-25 03:48:14 +02:00
# @raise [SystemCallError]
# Might raise a subclass of SystemCallError
# @return [Boolean]
2024-06-25 05:52:16 +02:00
# Returns true when successful
2017-05-24 02:18:47 +02:00
def enter!
2024-06-25 09:37:54 +02:00
return true unless FFI.cap_enter == -1
2024-06-25 05:52:16 +02:00
raise SystemCallError.new("cap_enter", Fiddle.last_error)
2017-05-24 02:18:47 +02:00
end
2017-05-24 02:18:05 +02:00
end