Class: CapNG::State

Inherits:
Object
  • Object
show all
Defined in:
ext/capng/state.c,
ext/capng/state.c

Overview

Handle CapNG state.

Examples:

require 'capng'

@state = CapNG::State.new
@state.save
# Some capability operations
@state.restore

Instance Method Summary collapse

Constructor Details

#initializeObject



84
85
86
87
88
89
90
91
92
93
# File 'ext/capng/state.c', line 84

static VALUE
rb_capng_state_initialize(VALUE self)
{
  struct CapNGState* capng_state;

  TypedData_Get_Struct(self, struct CapNGState, &rb_capng_state_type, capng_state);

  capng_state->state = NULL;
  return Qnil;
}

Instance Method Details

#restoreObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/capng/state.c', line 126

static VALUE
rb_capng_state_restore(VALUE self)
{
  struct CapNGState* capng_state;

  TypedData_Get_Struct(self, struct CapNGState, &rb_capng_state_type, capng_state);

  /* Pass the struct field itself so capng_restore_state() frees the block and
   * resets our pointer to NULL. Passing a local copy (the previous behaviour)
   * left capng_state->state dangling, causing a use-after-free / double-free on
   * a second #restore. With the field NULLed, a repeated #restore is a safe
   * no-op because libcap-ng ignores a NULL saved state. */
  capng_restore_state(&capng_state->state);

  return Qnil;
}

#saveObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/capng/state.c', line 101

static VALUE
rb_capng_state_save(VALUE self)
{
  struct CapNGState* capng_state;

  TypedData_Get_Struct(self, struct CapNGState, &rb_capng_state_type, capng_state);

  /* Free any previously saved state so repeated #save does not leak. */
  if (capng_state->state) {
    free(capng_state->state);
    capng_state->state = NULL;
  }

  capng_state->state = capng_save_state();

  return Qnil;
}