Module: Seccomp::Notify::Libc

Defined in:
lib/seccomp/notify/libc.rb

Constant Summary collapse

HANDLE =
Fiddle.dlopen(nil)
SYSCALL =
Fiddle::Function.new(
  HANDLE["syscall"],
  [Fiddle::TYPE_LONG] * 4,
  Fiddle::TYPE_LONG,
  need_gvl: true
)
PRCTL =
Fiddle::Function.new(
  HANDLE["prctl"],
  [Fiddle::TYPE_INT] * 5,
  Fiddle::TYPE_INT
)
IOCTL =
Fiddle::Function.new(
  HANDLE["ioctl"],
  [Fiddle::TYPE_INT, Fiddle::TYPE_LONG, Fiddle::TYPE_VOIDP],
  Fiddle::TYPE_INT
)
POLL =
Fiddle::Function.new(
  HANDLE["poll"],
  [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG, Fiddle::TYPE_INT],
  Fiddle::TYPE_INT
)

Class Method Summary collapse

Class Method Details

.architectureObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/seccomp/notify/libc.rb', line 33

def architecture
  @architecture ||= case RUBY_PLATFORM
  when /x86_64/
    :x86_64
  when /aarch64|arm64/
    :aarch64
  else
    raise NotSupportedError, "unsupported architecture: #{RUBY_PLATFORM}"
  end
end

.ioctl(fd, request, buffer) ⇒ Object



61
62
63
# File 'lib/seccomp/notify/libc.rb', line 61

def ioctl(fd, request, buffer)
  with_pointer(buffer) { |pointer| IOCTL.call(fd, request, pointer) }
end

.notif_sizesObject

Raises:

  • (SystemCallError)


74
75
76
77
78
79
80
81
# File 'lib/seccomp/notify/libc.rb', line 74

def notif_sizes
  buffer = "\0" * 6
  result = seccomp(Constants::SECCOMP_GET_NOTIF_SIZES, 0, buffer)
  raise SystemCallError.new("seccomp(GET_NOTIF_SIZES)", Fiddle.last_error) if result.negative?

  notif, resp, data = buffer.unpack("S<S<S<")
  {notif: notif, resp: resp, data: data}.freeze
end

.poll_hup?(fd) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (SystemCallError)


65
66
67
68
69
70
71
72
# File 'lib/seccomp/notify/libc.rb', line 65

def poll_hup?(fd)
  buffer = [fd, 1, 0].pack("l<s<s<")
  result = with_pointer(buffer) { |pointer| POLL.call(pointer, 1, 0) }
  raise SystemCallError.new("poll", Fiddle.last_error) if result.negative?

  revents = buffer.unpack1("@6S<")
  (revents & 0x10).positive? && (revents & 0x01).zero?
end

.prctl(option, arg2 = 0, arg3 = 0, arg4 = 0, arg5 = 0) ⇒ Object



44
45
46
# File 'lib/seccomp/notify/libc.rb', line 44

def prctl(option, arg2 = 0, arg3 = 0, arg4 = 0, arg5 = 0)
  PRCTL.call(option, arg2, arg3, arg4, arg5)
end

.seccomp(operation, flags, buffer = 0) ⇒ Object



48
49
50
51
52
# File 'lib/seccomp/notify/libc.rb', line 48

def seccomp(operation, flags, buffer = 0)
  with_pointer(buffer) do |pointer|
    SYSCALL.call(Constants::SYS_SECCOMP.fetch(architecture), operation, flags, pointer.to_i)
  end
end

.tgkill(tgid, tid, signal) ⇒ Object

Raises:

  • (SystemCallError)


54
55
56
57
58
59
# File 'lib/seccomp/notify/libc.rb', line 54

def tgkill(tgid, tid, signal)
  result = SYSCALL.call(Constants::SYS_TGKILL.fetch(architecture), tgid, tid, signal)
  raise SystemCallError.new("tgkill", Fiddle.last_error) if result.negative?

  result
end