2024-06-24 03:38:24 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-24 02:18:05 +02:00
|
|
|
module Capsicum
|
2024-06-25 05:08:23 +02:00
|
|
|
require_relative "capsicum/version"
|
2024-06-25 05:07:00 +02:00
|
|
|
require_relative "capsicum/libc"
|
2024-06-25 05:42:37 +02:00
|
|
|
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.
|
|
|
|
#
|
|
|
|
# @see 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
|
2024-06-25 03:56:42 +02:00
|
|
|
def in_capability_mode?
|
2024-06-23 23:18:33 +02:00
|
|
|
uintp = Fiddle::Pointer.malloc(Fiddle::SIZEOF_UINT)
|
|
|
|
ret = LibC.cap_getmode(uintp)
|
2017-05-24 02:18:47 +02:00
|
|
|
|
|
|
|
if ret == 0
|
2024-06-24 03:38:24 +02:00
|
|
|
uintp[0, Fiddle::SIZEOF_UINT].unpack("i") == [1]
|
2017-05-24 02:18:47 +02:00
|
|
|
else
|
2024-06-23 23:18:33 +02:00
|
|
|
raise SystemCallError.new("cap_getmode", Fiddle.last_error)
|
2017-05-24 02:18:47 +02:00
|
|
|
end
|
2024-06-23 23:18:33 +02:00
|
|
|
ensure
|
|
|
|
uintp.call_free
|
2017-05-24 02:18:47 +02:00
|
|
|
end
|
2024-06-25 05:42:37 +02:00
|
|
|
alias_method :capability_mode?, :in_capability_mode?
|
2017-05-24 02:18:47 +02:00
|
|
|
|
2024-06-25 03:48:14 +02:00
|
|
|
##
|
|
|
|
# Enter capability mode
|
2017-05-24 17:33:39 +02:00
|
|
|
#
|
|
|
|
# @see cap_enter(2)
|
2024-06-25 03:48:14 +02:00
|
|
|
# @raise [SystemCallError]
|
|
|
|
# Might raise a subclass of SystemCallError
|
|
|
|
# @return [Boolean]
|
|
|
|
# Returns true when the current process is in capability mode
|
2017-05-24 02:18:47 +02:00
|
|
|
def enter!
|
2024-06-25 03:48:14 +02:00
|
|
|
if LibC.cap_enter == 0
|
2024-06-24 03:38:24 +02:00
|
|
|
true
|
2017-05-24 02:18:47 +02:00
|
|
|
else
|
2024-06-23 23:18:33 +02:00
|
|
|
raise SystemCallError.new("cap_enter", Fiddle.last_error)
|
2017-05-24 02:18:47 +02:00
|
|
|
end
|
|
|
|
end
|
2017-05-24 02:18:05 +02:00
|
|
|
end
|