github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/kernel/uts_namespace.go (about)

     1  // Copyright 2018 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 kernel
    16  
    17  import (
    18  	"github.com/metacubex/gvisor/pkg/context"
    19  	"github.com/metacubex/gvisor/pkg/sentry/fsimpl/nsfs"
    20  	"github.com/metacubex/gvisor/pkg/sentry/kernel/auth"
    21  	"github.com/metacubex/gvisor/pkg/sync"
    22  )
    23  
    24  // UTSNamespace represents a UTS namespace, a holder of two system identifiers:
    25  // the hostname and domain name.
    26  //
    27  // +stateify savable
    28  type UTSNamespace struct {
    29  	// mu protects all fields below.
    30  	mu         sync.Mutex `state:"nosave"`
    31  	hostName   string
    32  	domainName string
    33  
    34  	// userns is the user namespace associated with the UTSNamespace.
    35  	// Privileged operations on this UTSNamespace must have appropriate
    36  	// capabilities in userns.
    37  	//
    38  	// userns is immutable.
    39  	userns *auth.UserNamespace
    40  
    41  	inode *nsfs.Inode
    42  }
    43  
    44  // NewUTSNamespace creates a new UTS namespace.
    45  func NewUTSNamespace(hostName, domainName string, userns *auth.UserNamespace) *UTSNamespace {
    46  	return &UTSNamespace{
    47  		hostName:   hostName,
    48  		domainName: domainName,
    49  		userns:     userns,
    50  	}
    51  }
    52  
    53  // UTSNamespace returns the task's UTS namespace.
    54  func (t *Task) UTSNamespace() *UTSNamespace {
    55  	t.mu.Lock()
    56  	defer t.mu.Unlock()
    57  	return t.utsns
    58  }
    59  
    60  // GetUTSNamespace takes a reference on the task UTS namespace and
    61  // returns it. It will return nil if the task isn't alive.
    62  func (t *Task) GetUTSNamespace() *UTSNamespace {
    63  	t.mu.Lock()
    64  	defer t.mu.Unlock()
    65  	if t.utsns != nil {
    66  		t.utsns.IncRef()
    67  	}
    68  	return t.utsns
    69  }
    70  
    71  // HostName returns the host name of this UTS namespace.
    72  func (u *UTSNamespace) HostName() string {
    73  	u.mu.Lock()
    74  	defer u.mu.Unlock()
    75  	return u.hostName
    76  }
    77  
    78  // SetHostName sets the host name of this UTS namespace.
    79  func (u *UTSNamespace) SetHostName(host string) {
    80  	u.mu.Lock()
    81  	defer u.mu.Unlock()
    82  	u.hostName = host
    83  }
    84  
    85  // DomainName returns the domain name of this UTS namespace.
    86  func (u *UTSNamespace) DomainName() string {
    87  	u.mu.Lock()
    88  	defer u.mu.Unlock()
    89  	return u.domainName
    90  }
    91  
    92  // SetDomainName sets the domain name of this UTS namespace.
    93  func (u *UTSNamespace) SetDomainName(domain string) {
    94  	u.mu.Lock()
    95  	defer u.mu.Unlock()
    96  	u.domainName = domain
    97  }
    98  
    99  // UserNamespace returns the user namespace associated with this UTS namespace.
   100  func (u *UTSNamespace) UserNamespace() *auth.UserNamespace {
   101  	u.mu.Lock()
   102  	defer u.mu.Unlock()
   103  	return u.userns
   104  }
   105  
   106  // Type implements nsfs.Namespace.Type.
   107  func (u *UTSNamespace) Type() string {
   108  	return "uts"
   109  }
   110  
   111  // Destroy implements nsfs.Namespace.Destroy.
   112  func (u *UTSNamespace) Destroy(ctx context.Context) {}
   113  
   114  // SetInode sets the nsfs `inode` to the UTS namespace.
   115  func (u *UTSNamespace) SetInode(inode *nsfs.Inode) {
   116  	u.mu.Lock()
   117  	defer u.mu.Unlock()
   118  	u.inode = inode
   119  }
   120  
   121  // GetInode returns the nsfs inode associated with the UTS  namespace.
   122  func (u *UTSNamespace) GetInode() *nsfs.Inode {
   123  	u.mu.Lock()
   124  	defer u.mu.Unlock()
   125  	return u.inode
   126  }
   127  
   128  // IncRef increments the Namespace's refcount.
   129  func (u *UTSNamespace) IncRef() {
   130  	u.mu.Lock()
   131  	defer u.mu.Unlock()
   132  	u.inode.IncRef()
   133  }
   134  
   135  // DecRef decrements the namespace's refcount.
   136  func (u *UTSNamespace) DecRef(ctx context.Context) {
   137  	u.mu.Lock()
   138  	defer u.mu.Unlock()
   139  	u.inode.DecRef(ctx)
   140  }
   141  
   142  // Clone makes a copy of this UTS namespace, associating the given user
   143  // namespace.
   144  func (u *UTSNamespace) Clone(userns *auth.UserNamespace) *UTSNamespace {
   145  	u.mu.Lock()
   146  	defer u.mu.Unlock()
   147  	return &UTSNamespace{
   148  		hostName:   u.hostName,
   149  		domainName: u.domainName,
   150  		userns:     userns,
   151  	}
   152  }