Class: Haconiwa::SmallLibcap

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/haconiwa/small_libcap.rb

Defined Under Namespace

Classes: CapError

Class Method Summary collapse

Class Method Details

._name2cap(name) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/haconiwa/small_libcap.rb', line 31

def self._name2cap(name)
  ptr = FFI::MemoryPointer.new(:int)
  err = cap_from_name(name, ptr)
  if err < 0
    raise CapError, "Invalid or unsupported capability name: #{name}"
  end
  ptr.read_int
end

.apply_cap_whitelist(list: []) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/haconiwa/small_libcap.rb', line 48

def self.apply_cap_whitelist(list: [])
  whitelist = list.map{|n| _name2cap(n) }

  loop.with_index(0) do |_, cap_value|
    return(true) unless cap_supported?(cap_value)
    next if whitelist.include?(cap_value)

    err = cap_drop_bound(cap_value)
    if err < 0
      raise CapError, "Failed to drop capability cap_value_t: #{cap_value} from bounding set"
    end
  end
end

.cap_supported?(cap) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/haconiwa/small_libcap.rb', line 27

def self.cap_supported?(cap)
  cap_get_bound(cap) >= 0
end

.drop_cap_by_name(name) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/haconiwa/small_libcap.rb', line 40

def self.drop_cap_by_name(name)
  err = cap_drop_bound(_name2cap(name))
  if err < 0
    raise CapError, "Failed to drop capability name: #{name} from bounding set"
  end
  true
end