github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/syscalls/linux/timespec.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 linux
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    21  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    22  	"github.com/SagerNet/gvisor/pkg/hostarch"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    24  	"github.com/SagerNet/gvisor/pkg/syserror"
    25  )
    26  
    27  // copyTimespecIn copies a Timespec from the untrusted app range to the kernel.
    28  func copyTimespecIn(t *kernel.Task, addr hostarch.Addr) (linux.Timespec, error) {
    29  	switch t.Arch().Width() {
    30  	case 8:
    31  		ts := linux.Timespec{}
    32  		in := t.CopyScratchBuffer(16)
    33  		_, err := t.CopyInBytes(addr, in)
    34  		if err != nil {
    35  			return ts, err
    36  		}
    37  		ts.Sec = int64(hostarch.ByteOrder.Uint64(in[0:]))
    38  		ts.Nsec = int64(hostarch.ByteOrder.Uint64(in[8:]))
    39  		return ts, nil
    40  	default:
    41  		return linux.Timespec{}, syserror.ENOSYS
    42  	}
    43  }
    44  
    45  // copyTimespecOut copies a Timespec to the untrusted app range.
    46  func copyTimespecOut(t *kernel.Task, addr hostarch.Addr, ts *linux.Timespec) error {
    47  	switch t.Arch().Width() {
    48  	case 8:
    49  		out := t.CopyScratchBuffer(16)
    50  		hostarch.ByteOrder.PutUint64(out[0:], uint64(ts.Sec))
    51  		hostarch.ByteOrder.PutUint64(out[8:], uint64(ts.Nsec))
    52  		_, err := t.CopyOutBytes(addr, out)
    53  		return err
    54  	default:
    55  		return syserror.ENOSYS
    56  	}
    57  }
    58  
    59  // copyTimevalIn copies a Timeval from the untrusted app range to the kernel.
    60  func copyTimevalIn(t *kernel.Task, addr hostarch.Addr) (linux.Timeval, error) {
    61  	switch t.Arch().Width() {
    62  	case 8:
    63  		tv := linux.Timeval{}
    64  		in := t.CopyScratchBuffer(16)
    65  		_, err := t.CopyInBytes(addr, in)
    66  		if err != nil {
    67  			return tv, err
    68  		}
    69  		tv.Sec = int64(hostarch.ByteOrder.Uint64(in[0:]))
    70  		tv.Usec = int64(hostarch.ByteOrder.Uint64(in[8:]))
    71  		return tv, nil
    72  	default:
    73  		return linux.Timeval{}, syserror.ENOSYS
    74  	}
    75  }
    76  
    77  // copyTimevalOut copies a Timeval to the untrusted app range.
    78  func copyTimevalOut(t *kernel.Task, addr hostarch.Addr, tv *linux.Timeval) error {
    79  	switch t.Arch().Width() {
    80  	case 8:
    81  		out := t.CopyScratchBuffer(16)
    82  		hostarch.ByteOrder.PutUint64(out[0:], uint64(tv.Sec))
    83  		hostarch.ByteOrder.PutUint64(out[8:], uint64(tv.Usec))
    84  		_, err := t.CopyOutBytes(addr, out)
    85  		return err
    86  	default:
    87  		return syserror.ENOSYS
    88  	}
    89  }
    90  
    91  // copyTimespecInToDuration copies a Timespec from the untrusted app range,
    92  // validates it and converts it to a Duration.
    93  //
    94  // If the Timespec is larger than what can be represented in a Duration, the
    95  // returned value is the maximum that Duration will allow.
    96  //
    97  // If timespecAddr is NULL, the returned value is negative.
    98  func copyTimespecInToDuration(t *kernel.Task, timespecAddr hostarch.Addr) (time.Duration, error) {
    99  	// Use a negative Duration to indicate "no timeout".
   100  	timeout := time.Duration(-1)
   101  	if timespecAddr != 0 {
   102  		timespec, err := copyTimespecIn(t, timespecAddr)
   103  		if err != nil {
   104  			return 0, err
   105  		}
   106  		if !timespec.Valid() {
   107  			return 0, linuxerr.EINVAL
   108  		}
   109  		timeout = time.Duration(timespec.ToNsecCapped())
   110  	}
   111  	return timeout, nil
   112  }