summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/syscalls/linux/timespec.go
blob: e865c6fc018ccb880980f006f833f8cf27eaa56e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package linux

import (
	"syscall"
	"time"

	"gvisor.googlesource.com/gvisor/pkg/abi/linux"
	"gvisor.googlesource.com/gvisor/pkg/sentry/kernel"
	"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
	"gvisor.googlesource.com/gvisor/pkg/syserror"
)

// copyTimespecIn copies a Timespec from the untrusted app range to the kernel.
func copyTimespecIn(t *kernel.Task, addr usermem.Addr) (linux.Timespec, error) {
	switch t.Arch().Width() {
	case 8:
		ts := linux.Timespec{}
		in := t.CopyScratchBuffer(16)
		_, err := t.CopyInBytes(addr, in)
		if err != nil {
			return ts, err
		}
		ts.Sec = int64(usermem.ByteOrder.Uint64(in[0:]))
		ts.Nsec = int64(usermem.ByteOrder.Uint64(in[8:]))
		return ts, nil
	default:
		return linux.Timespec{}, syserror.ENOSYS
	}
}

// copyTimespecOut copies a Timespec to the untrusted app range.
func copyTimespecOut(t *kernel.Task, addr usermem.Addr, ts *linux.Timespec) error {
	switch t.Arch().Width() {
	case 8:
		out := t.CopyScratchBuffer(16)
		usermem.ByteOrder.PutUint64(out[0:], uint64(ts.Sec))
		usermem.ByteOrder.PutUint64(out[8:], uint64(ts.Nsec))
		_, err := t.CopyOutBytes(addr, out)
		return err
	default:
		return syserror.ENOSYS
	}
}

// copyTimevalIn copies a Timeval from the untrusted app range to the kernel.
func copyTimevalIn(t *kernel.Task, addr usermem.Addr) (linux.Timeval, error) {
	switch t.Arch().Width() {
	case 8:
		tv := linux.Timeval{}
		in := t.CopyScratchBuffer(16)
		_, err := t.CopyInBytes(addr, in)
		if err != nil {
			return tv, err
		}
		tv.Sec = int64(usermem.ByteOrder.Uint64(in[0:]))
		tv.Usec = int64(usermem.ByteOrder.Uint64(in[8:]))
		return tv, nil
	default:
		return linux.Timeval{}, syscall.ENOSYS
	}
}

// copyTimevalOut copies a Timeval to the untrusted app range.
func copyTimevalOut(t *kernel.Task, addr usermem.Addr, tv *linux.Timeval) error {
	switch t.Arch().Width() {
	case 8:
		out := t.CopyScratchBuffer(16)
		usermem.ByteOrder.PutUint64(out[0:], uint64(tv.Sec))
		usermem.ByteOrder.PutUint64(out[8:], uint64(tv.Usec))
		_, err := t.CopyOutBytes(addr, out)
		return err
	default:
		return syscall.ENOSYS
	}
}

// copyTimespecInToDuration copies a Timespec from the untrusted app range,
// validates it and converts it to a Duration.
//
// If the Timespec is larger than what can be represented in a Duration, the
// returned value is the maximum that Duration will allow.
//
// If timespecAddr is NULL, the returned value is negative.
func copyTimespecInToDuration(t *kernel.Task, timespecAddr usermem.Addr) (time.Duration, error) {
	// Use a negative Duration to indicate "no timeout".
	timeout := time.Duration(-1)
	if timespecAddr != 0 {
		timespec, err := copyTimespecIn(t, timespecAddr)
		if err != nil {
			return 0, err
		}
		if !timespec.Valid() {
			return 0, syscall.EINVAL
		}
		timeout = time.Duration(timespec.ToNsecCapped())
	}
	return timeout, nil
}