Skip to content

Commit

Permalink
apparmor: simplify IoctlRequestBuffer
Browse files Browse the repository at this point in the history
The `IoctlRequestBuffer` is currently a struct with a single
`[]byte`. This means it might equally be just an alias type
with the needed methods.
  • Loading branch information
mvo5 committed Sep 7, 2023
1 parent b78e54a commit 4c9483e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
33 changes: 10 additions & 23 deletions sandbox/apparmor/notify/ioctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,36 @@ func (hb hexBuf) String() string {

// IoctlRequestBuffer is a buffer with a constructor method which prepares a
// buffer to be passed into ioctl(2), along with other useful methods.
type IoctlRequestBuffer struct {
bytes []byte
}
type IoctlRequestBuffer []byte

// NewIoctlRequestBuffer returns a new buffer for communication with the kernel.
//
// The buffer contains encoded information about its size and protocol version.
func NewIoctlRequestBuffer() *IoctlRequestBuffer {
func NewIoctlRequestBuffer() IoctlRequestBuffer {
bufSize := 0xFFFF
buf := bytes.NewBuffer(make([]byte, 0, bufSize))
header := MsgHeader{Version: 3, Length: uint16(bufSize)}
order := arch.Endian()
binary.Write(buf, order, &header)
buf.Write(make([]byte, bufSize-buf.Len()))
return &IoctlRequestBuffer{
bytes: buf.Bytes(),
}
}

// BytesToIoctlRequestBuffer creates a new IoctlRequestBuffer built around the
// given buffer. The buffer should the output of a valid ioctl message struct
// being marshalled into bytes.
func BytesToIoctlRequestBuffer(buf []byte) *IoctlRequestBuffer {
return &IoctlRequestBuffer{
bytes: buf,
}
return IoctlRequestBuffer(buf.Bytes())
}

// Bytes returns the underlying byte slice of an IoctlRequestBuffer.
func (b *IoctlRequestBuffer) Bytes() []byte {
return b.bytes
func (b IoctlRequestBuffer) Bytes() []byte {
return b
}

// Len returns the length of the underlying byte slice of an IoctlRequestBuffer.
func (b *IoctlRequestBuffer) Len() int {
return len(b.bytes)
func (b IoctlRequestBuffer) Len() int {
return len(b)
}

// Pointer returns a pointer to the first element of an IoctlRequestBuffer.
//
// This is intended to be used to simplify passing the buffer into ioctl(2).
func (b *IoctlRequestBuffer) Pointer() uintptr {
return uintptr(unsafe.Pointer(&b.bytes[0]))
func (b IoctlRequestBuffer) Pointer() uintptr {
return uintptr(unsafe.Pointer(&b[0]))
}

var dumpIoctl bool = osutil.GetenvBool("SNAPD_DEBUG_DUMP_IOCTL")
Expand All @@ -103,7 +90,7 @@ func SetIoctlDump(value bool) bool {
// Ioctl calls. If the ioctl syscall returns an error, the buffer contents are
// those filled by the syscall, but the size of the buffer is unchanged, and
// the complete buffer and error are returned.
func Ioctl(fd uintptr, req IoctlRequest, buf *IoctlRequestBuffer) ([]byte, error) {
func Ioctl(fd uintptr, req IoctlRequest, buf IoctlRequestBuffer) ([]byte, error) {
var err error
if dumpIoctl {
log.Printf(">>> ioctl %v (%d bytes) ...\n", req, buf.Len())
Expand Down
2 changes: 1 addition & 1 deletion sandbox/apparmor/notify/ioctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (*ioctlSuite) TestIoctlRequestBuffer(c *C) {

func (*ioctlSuite) TestBytesToIoctlRequestBuffer(c *C) {
buf := []byte("foo")
ioctlBuf := notify.BytesToIoctlRequestBuffer(buf)
ioctlBuf := notify.IoctlRequestBuffer(buf)
c.Assert(ioctlBuf.Bytes(), DeepEquals, buf)
}

Expand Down

0 comments on commit 4c9483e

Please sign in to comment.