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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
// Copyright 2018 Google LLC
//
// 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 (
"gvisor.googlesource.com/gvisor/pkg/abi/linux"
"gvisor.googlesource.com/gvisor/pkg/sentry/arch"
"gvisor.googlesource.com/gvisor/pkg/sentry/kernel"
"gvisor.googlesource.com/gvisor/pkg/sentry/limits"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
"gvisor.googlesource.com/gvisor/pkg/syserror"
)
// rlimit describes an implementation of 'struct rlimit', which may vary from
// system-to-system.
type rlimit interface {
// toLimit converts an rlimit to a limits.Limit.
toLimit() *limits.Limit
// fromLimit converts a limits.Limit to an rlimit.
fromLimit(lim limits.Limit)
// copyIn copies an rlimit from the untrusted app to the kernel.
copyIn(t *kernel.Task, addr usermem.Addr) error
// copyOut copies an rlimit from the kernel to the untrusted app.
copyOut(t *kernel.Task, addr usermem.Addr) error
}
// newRlimit returns the appropriate rlimit type for 'struct rlimit' on this system.
func newRlimit(t *kernel.Task) (rlimit, error) {
switch t.Arch().Width() {
case 8:
// On 64-bit system, struct rlimit and struct rlimit64 are identical.
return &rlimit64{}, nil
default:
return nil, syserror.ENOSYS
}
}
type rlimit64 struct {
Cur uint64
Max uint64
}
func (r *rlimit64) toLimit() *limits.Limit {
return &limits.Limit{
Cur: limits.FromLinux(r.Cur),
Max: limits.FromLinux(r.Max),
}
}
func (r *rlimit64) fromLimit(lim limits.Limit) {
*r = rlimit64{
Cur: limits.ToLinux(lim.Cur),
Max: limits.ToLinux(lim.Max),
}
}
func (r *rlimit64) copyIn(t *kernel.Task, addr usermem.Addr) error {
_, err := t.CopyIn(addr, r)
return err
}
func (r *rlimit64) copyOut(t *kernel.Task, addr usermem.Addr) error {
_, err := t.CopyOut(addr, *r)
return err
}
func makeRlimit64(lim limits.Limit) *rlimit64 {
return &rlimit64{Cur: lim.Cur, Max: lim.Max}
}
// setableLimits is the set of supported setable limits.
var setableLimits = map[limits.LimitType]struct{}{
limits.NumberOfFiles: {},
limits.AS: {},
limits.CPU: {},
limits.Data: {},
limits.FileSize: {},
limits.Stack: {},
// These are not enforced, but we include them here to avoid returning
// EPERM, since some apps expect them to succeed.
limits.Core: {},
limits.ProcessCount: {},
}
func prlimit64(t *kernel.Task, resource limits.LimitType, newLim *limits.Limit) (limits.Limit, error) {
if newLim == nil {
return t.ThreadGroup().Limits().Get(resource), nil
}
if _, ok := setableLimits[resource]; !ok {
return limits.Limit{}, syserror.EPERM
}
oldLim, err := t.ThreadGroup().Limits().Set(resource, *newLim)
if err != nil {
return limits.Limit{}, err
}
if resource == limits.CPU {
t.NotifyRlimitCPUUpdated()
}
return oldLim, nil
}
// Getrlimit implements linux syscall getrlimit(2).
func Getrlimit(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
resource, ok := limits.FromLinuxResource[int(args[0].Int())]
if !ok {
// Return err; unknown limit.
return 0, nil, syserror.EINVAL
}
addr := args[1].Pointer()
rlim, err := newRlimit(t)
if err != nil {
return 0, nil, err
}
lim, err := prlimit64(t, resource, nil)
if err != nil {
return 0, nil, err
}
rlim.fromLimit(lim)
return 0, nil, rlim.copyOut(t, addr)
}
// Setrlimit implements linux syscall setrlimit(2).
func Setrlimit(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
resource, ok := limits.FromLinuxResource[int(args[0].Int())]
if !ok {
// Return err; unknown limit.
return 0, nil, syserror.EINVAL
}
addr := args[1].Pointer()
rlim, err := newRlimit(t)
if err != nil {
return 0, nil, err
}
if err := rlim.copyIn(t, addr); err != nil {
return 0, nil, syserror.EFAULT
}
_, err = prlimit64(t, resource, rlim.toLimit())
return 0, nil, err
}
// Prlimit64 implements linux syscall prlimit64(2).
func Prlimit64(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
tid := kernel.ThreadID(args[0].Int())
resource, ok := limits.FromLinuxResource[int(args[1].Int())]
if !ok {
// Return err; unknown limit.
return 0, nil, syserror.EINVAL
}
newRlimAddr := args[2].Pointer()
oldRlimAddr := args[3].Pointer()
var newLim *limits.Limit
if newRlimAddr != 0 {
var nrl rlimit64
if err := nrl.copyIn(t, newRlimAddr); err != nil {
return 0, nil, syserror.EFAULT
}
newLim = nrl.toLimit()
}
if tid < 0 {
return 0, nil, syserror.EINVAL
}
ot := t
if tid > 0 {
if ot = t.PIDNamespace().TaskWithID(tid); ot == nil {
return 0, nil, syserror.ESRCH
}
}
// "To set or get the resources of a process other than itself, the caller
// must have the CAP_SYS_RESOURCE capability, or the real, effective, and
// saved set user IDs of the target process must match the real user ID of
// the caller and the real, effective, and saved set group IDs of the
// target process must match the real group ID of the caller."
if !t.HasCapabilityIn(linux.CAP_SYS_RESOURCE, t.PIDNamespace().UserNamespace()) {
cred, tcred := t.Credentials(), ot.Credentials()
if cred.RealKUID != tcred.RealKUID ||
cred.RealKUID != tcred.EffectiveKUID ||
cred.RealKUID != tcred.SavedKUID ||
cred.RealKGID != tcred.RealKGID ||
cred.RealKGID != tcred.EffectiveKGID ||
cred.RealKGID != tcred.SavedKGID {
return 0, nil, syserror.EPERM
}
}
oldLim, err := prlimit64(ot, resource, newLim)
if err != nil {
return 0, nil, err
}
if oldRlimAddr != 0 {
if err := makeRlimit64(oldLim).copyOut(t, oldRlimAddr); err != nil {
return 0, nil, syserror.EFAULT
}
}
return 0, nil, nil
}
|