github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/syscalls/linux/vfs2/memfd.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package vfs2
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    20  	"github.com/SagerNet/gvisor/pkg/sentry/arch"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fsimpl/tmpfs"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    23  )
    24  
    25  const (
    26  	memfdPrefix     = "memfd:"
    27  	memfdMaxNameLen = linux.NAME_MAX - len(memfdPrefix)
    28  	memfdAllFlags   = uint32(linux.MFD_CLOEXEC | linux.MFD_ALLOW_SEALING)
    29  )
    30  
    31  // MemfdCreate implements the linux syscall memfd_create(2).
    32  func MemfdCreate(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    33  	addr := args[0].Pointer()
    34  	flags := args[1].Uint()
    35  
    36  	if flags&^memfdAllFlags != 0 {
    37  		// Unknown bits in flags.
    38  		return 0, nil, linuxerr.EINVAL
    39  	}
    40  
    41  	allowSeals := flags&linux.MFD_ALLOW_SEALING != 0
    42  	cloExec := flags&linux.MFD_CLOEXEC != 0
    43  
    44  	name, err := t.CopyInString(addr, memfdMaxNameLen)
    45  	if err != nil {
    46  		return 0, nil, err
    47  	}
    48  
    49  	shmMount := t.Kernel().ShmMount()
    50  	file, err := tmpfs.NewMemfd(t, t.Credentials(), shmMount, allowSeals, memfdPrefix+name)
    51  	if err != nil {
    52  		return 0, nil, err
    53  	}
    54  	defer file.DecRef(t)
    55  
    56  	fd, err := t.NewFDFromVFS2(0, file, kernel.FDFlags{
    57  		CloseOnExec: cloExec,
    58  	})
    59  	if err != nil {
    60  		return 0, nil, err
    61  	}
    62  
    63  	return uintptr(fd), nil, nil
    64  }