|
| 1 | +// Copyright 2025 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 proc |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "gvisor.dev/gvisor/pkg/abi/linux" |
| 22 | + "gvisor.dev/gvisor/pkg/atomicbitops" |
| 23 | + "gvisor.dev/gvisor/pkg/context" |
| 24 | + "gvisor.dev/gvisor/pkg/errors/linuxerr" |
| 25 | + "gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs" |
| 26 | + "gvisor.dev/gvisor/pkg/sentry/kernel" |
| 27 | + "gvisor.dev/gvisor/pkg/sentry/kernel/auth" |
| 28 | + "gvisor.dev/gvisor/pkg/sentry/vfs" |
| 29 | + "gvisor.dev/gvisor/pkg/usermem" |
| 30 | +) |
| 31 | + |
| 32 | +func (fs *filesystem) newMaxKeySizeFile(ctx context.Context, k *kernel.Kernel, creds *auth.Credentials) kernfs.Inode { |
| 33 | + s := &maxKeySize{maxKeys: &k.MaxKeySetSize} |
| 34 | + s.Init(ctx, creds, linux.UNNAMED_MAJOR, fs.devMinor, fs.NextIno(), s, 0644) |
| 35 | + return s |
| 36 | +} |
| 37 | + |
| 38 | +// maxKeySize implements vfs.WritableDynamicBytesSource for |
| 39 | +// /proc/sys/kernel/keys/maxkeys. |
| 40 | +// |
| 41 | +// +stateify savable |
| 42 | +type maxKeySize struct { |
| 43 | + kernfs.DynamicBytesFile |
| 44 | + |
| 45 | + // maxKeys is the maximum number of keys allowed in a key set. |
| 46 | + maxKeys *atomicbitops.Int32 |
| 47 | +} |
| 48 | + |
| 49 | +var _ vfs.WritableDynamicBytesSource = (*maxKeySize)(nil) |
| 50 | + |
| 51 | +// Generate implements vfs.DynamicBytesSource.Generate. |
| 52 | +func (s *maxKeySize) Generate(ctx context.Context, buf *bytes.Buffer) error { |
| 53 | + _, err := fmt.Fprintf(buf, "%d\n", s.maxKeys.Load()) |
| 54 | + return err |
| 55 | +} |
| 56 | + |
| 57 | +// Write implements vfs.WritableDynamicBytesSource.Write. |
| 58 | +func (s *maxKeySize) Write(ctx context.Context, _ *vfs.FileDescription, src usermem.IOSequence, offset int64) (int64, error) { |
| 59 | + if offset != 0 { |
| 60 | + // Ignore partial writes. |
| 61 | + return 0, linuxerr.EINVAL |
| 62 | + } |
| 63 | + if !auth.CredentialsFromContext(ctx).HasCapability(linux.CAP_SYS_ADMIN) { |
| 64 | + return 0, linuxerr.EPERM |
| 65 | + } |
| 66 | + buf := make([]int32, 1) |
| 67 | + n, err := ParseInt32Vec(ctx, src, buf) |
| 68 | + if err != nil || n == 0 { |
| 69 | + return 0, err |
| 70 | + } |
| 71 | + |
| 72 | + if buf[0] <= 0 { |
| 73 | + return 0, linuxerr.EINVAL |
| 74 | + } |
| 75 | + |
| 76 | + s.maxKeys.Store(buf[0]) |
| 77 | + return n, nil |
| 78 | +} |
0 commit comments