mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Emit SentryTcpListenEvent when the application listens on a tcp port.
PiperOrigin-RevId: 562957239
This commit is contained in:
committed by
gVisor bot
parent
e0029cc0ff
commit
c227d185a4
@@ -1,4 +1,4 @@
|
||||
load("//tools:defs.bzl", "go_library")
|
||||
load("//tools:defs.bzl", "go_library", "proto_library")
|
||||
|
||||
package(
|
||||
default_applicable_licenses = ["//:license"],
|
||||
@@ -19,10 +19,12 @@ go_library(
|
||||
"//pkg/sentry:internal",
|
||||
],
|
||||
deps = [
|
||||
":events_go_proto",
|
||||
"//pkg/abi/linux",
|
||||
"//pkg/abi/linux/errno",
|
||||
"//pkg/context",
|
||||
"//pkg/errors/linuxerr",
|
||||
"//pkg/eventchannel",
|
||||
"//pkg/hostarch",
|
||||
"//pkg/log",
|
||||
"//pkg/marshal",
|
||||
@@ -51,6 +53,13 @@ go_library(
|
||||
"//pkg/tcpip/transport/udp",
|
||||
"//pkg/usermem",
|
||||
"//pkg/waiter",
|
||||
"@org_golang_google_protobuf//proto:go_default_library",
|
||||
"@org_golang_x_sys//unix:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
proto_library(
|
||||
name = "events",
|
||||
srcs = ["events.proto"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2023 The gVisor Authors.
|
||||
//
|
||||
// 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.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package gvisor;
|
||||
|
||||
// SentryTcpListenEvent is emitted when the application successfully
|
||||
// listens on a netstack TCP socket.
|
||||
message SentryTcpListenEvent {
|
||||
// port is the port the socket is bound to.
|
||||
optional int32 port = 1;
|
||||
}
|
||||
@@ -35,10 +35,12 @@ import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"gvisor.dev/gvisor/pkg/abi/linux"
|
||||
"gvisor.dev/gvisor/pkg/abi/linux/errno"
|
||||
"gvisor.dev/gvisor/pkg/context"
|
||||
"gvisor.dev/gvisor/pkg/errors/linuxerr"
|
||||
"gvisor.dev/gvisor/pkg/eventchannel"
|
||||
"gvisor.dev/gvisor/pkg/hostarch"
|
||||
"gvisor.dev/gvisor/pkg/log"
|
||||
"gvisor.dev/gvisor/pkg/marshal"
|
||||
@@ -52,6 +54,7 @@ import (
|
||||
ktime "gvisor.dev/gvisor/pkg/sentry/kernel/time"
|
||||
"gvisor.dev/gvisor/pkg/sentry/socket"
|
||||
"gvisor.dev/gvisor/pkg/sentry/socket/netfilter"
|
||||
epb "gvisor.dev/gvisor/pkg/sentry/socket/netstack/events_go_proto"
|
||||
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
||||
"gvisor.dev/gvisor/pkg/sync"
|
||||
"gvisor.dev/gvisor/pkg/syserr"
|
||||
@@ -794,7 +797,22 @@ func (s *sock) Bind(_ *kernel.Task, sockaddr []byte) *syserr.Error {
|
||||
// Listen implements the linux syscall listen(2) for sockets backed by
|
||||
// tcpip.Endpoint.
|
||||
func (s *sock) Listen(_ *kernel.Task, backlog int) *syserr.Error {
|
||||
return syserr.TranslateNetstackError(s.Endpoint.Listen(backlog))
|
||||
if err := s.Endpoint.Listen(backlog); err != nil {
|
||||
return syserr.TranslateNetstackError(err)
|
||||
}
|
||||
if !socket.IsTCP(s) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Emit SentryTCPListenEvent with the bound port for tcp sockets.
|
||||
addr, err := s.Endpoint.GetLocalAddress()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("GetLocalAddress failed for tcp socket: %s", err))
|
||||
}
|
||||
eventchannel.Emit(&epb.SentryTcpListenEvent{
|
||||
Port: proto.Int32(int32(addr.Port)),
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// blockingAccept implements a blocking version of accept(2), that is, if no
|
||||
|
||||
@@ -2599,14 +2599,14 @@ func (e *endpoint) shutdownLocked(flags tcpip.ShutdownFlags) tcpip.Error {
|
||||
// Listen puts the endpoint in "listen" mode, which allows it to accept
|
||||
// new connections.
|
||||
func (e *endpoint) Listen(backlog int) tcpip.Error {
|
||||
err := e.listen(backlog)
|
||||
if err != nil {
|
||||
if err := e.listen(backlog); err != nil {
|
||||
if !err.IgnoreStats() {
|
||||
e.stack.Stats().TCP.FailedConnectionAttempts.Increment()
|
||||
e.stats.FailedConnectionAttempts.Increment()
|
||||
}
|
||||
return err
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *endpoint) listen(backlog int) tcpip.Error {
|
||||
|
||||
Reference in New Issue
Block a user