From a49cefe7f6f14073cc5c48cc4ab0e6e62d64bd50 Mon Sep 17 00:00:00 2001 From: ruitianzhong Date: Thu, 29 Feb 2024 11:19:49 +0800 Subject: [PATCH 1/5] avoid printing confusing message which contains special character like `%` Signed-off-by: ruitianzhong --- user/event/event_bash.go | 4 ++-- user/event/event_mysqld.go | 4 ++-- user/event/event_postgres.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/user/event/event_bash.go b/user/event/event_bash.go index 5f71ed315..b4787fddc 100644 --- a/user/event/event_bash.go +++ b/user/event/event_bash.go @@ -62,12 +62,12 @@ func (be *BashEvent) Decode(payload []byte) (err error) { } func (be *BashEvent) String() string { - s := fmt.Sprintf(fmt.Sprintf("PID:%d, UID:%d, \tComm:%s, \tRetvalue:%d, \tLine:\n%s", be.Pid, be.Uid, be.Comm, be.Retval, unix.ByteSliceToString((be.Line[:])))) + s := fmt.Sprintf("PID:%d, UID:%d, \tComm:%s, \tRetvalue:%d, \tLine:\n%s", be.Pid, be.Uid, be.Comm, be.Retval, unix.ByteSliceToString((be.Line[:]))) return s } func (be *BashEvent) StringHex() string { - s := fmt.Sprintf(fmt.Sprintf("PID:%d, UID:%d, \tComm:%s, \tRetvalue:%d, \tLine:\n%s,", be.Pid, be.Uid, be.Comm, be.Retval, dumpByteSlice([]byte(unix.ByteSliceToString((be.Line[:]))), ""))) + s := fmt.Sprintf("PID:%d, UID:%d, \tComm:%s, \tRetvalue:%d, \tLine:\n%s,", be.Pid, be.Uid, be.Comm, be.Retval, dumpByteSlice([]byte(unix.ByteSliceToString((be.Line[:]))), "")) return s } diff --git a/user/event/event_mysqld.go b/user/event/event_mysqld.go index 9908ae689..5716b415e 100644 --- a/user/event/event_mysqld.go +++ b/user/event/event_mysqld.go @@ -107,12 +107,12 @@ func (me *MysqldEvent) Decode(payload []byte) (err error) { } func (me *MysqldEvent) String() string { - s := fmt.Sprintf(fmt.Sprintf(" PID:%d, Comm:%s, Time:%d, length:(%d/%d), return:%s, Line:%s", me.Pid, me.Comm, me.Timestamp, me.Len, me.Alllen, me.Retval, unix.ByteSliceToString((me.Query[:])))) + s := fmt.Sprintf(" PID:%d, Comm:%s, Time:%d, length:(%d/%d), return:%s, Line:%s", me.Pid, me.Comm, me.Timestamp, me.Len, me.Alllen, me.Retval, unix.ByteSliceToString((me.Query[:]))) return s } func (me *MysqldEvent) StringHex() string { - s := fmt.Sprintf(fmt.Sprintf(" PID:%d, Comm:%s, Time:%d, length:(%d/%d), return:%s, Line:%s", me.Pid, me.Comm, me.Timestamp, me.Len, me.Alllen, me.Retval, unix.ByteSliceToString((me.Query[:])))) + s := fmt.Sprintf(" PID:%d, Comm:%s, Time:%d, length:(%d/%d), return:%s, Line:%s", me.Pid, me.Comm, me.Timestamp, me.Len, me.Alllen, me.Retval, unix.ByteSliceToString((me.Query[:]))) return s } diff --git a/user/event/event_postgres.go b/user/event/event_postgres.go index 305d58561..7c187d823 100644 --- a/user/event/event_postgres.go +++ b/user/event/event_postgres.go @@ -63,12 +63,12 @@ func (pe *PostgresEvent) Decode(payload []byte) (err error) { } func (pe *PostgresEvent) String() string { - s := fmt.Sprintf(fmt.Sprintf(" PID: %d, Comm: %s, Time: %d, Query: %s", pe.Pid, pe.Comm, pe.Timestamp, unix.ByteSliceToString((pe.Query[:])))) + s := fmt.Sprintf(" PID: %d, Comm: %s, Time: %d, Query: %s", pe.Pid, pe.Comm, pe.Timestamp, unix.ByteSliceToString((pe.Query[:]))) return s } func (pe *PostgresEvent) StringHex() string { - s := fmt.Sprintf(fmt.Sprintf(" PID: %d, Comm: %s, Time: %d, Query: %s", pe.Pid, pe.Comm, pe.Timestamp, unix.ByteSliceToString((pe.Query[:])))) + s := fmt.Sprintf(" PID: %d, Comm: %s, Time: %d, Query: %s", pe.Pid, pe.Comm, pe.Timestamp, unix.ByteSliceToString((pe.Query[:]))) return s } From 5b4a4636bc12bf507ad1b09022668f81c075011e Mon Sep 17 00:00:00 2001 From: ruitianzhong Date: Thu, 29 Feb 2024 15:13:43 +0800 Subject: [PATCH 2/5] add `staticcheck` for golangci-lint and fix related bugs Signed-off-by: ruitianzhong --- .github/workflows/go-c-cpp.yml | 2 +- pkg/util/ebpf/bpf_test.go | 4 ++-- user/config/config_gotls.go | 4 ++-- user/event/event_gotls.go | 4 +++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/go-c-cpp.yml b/.github/workflows/go-c-cpp.yml index 18bb56439..301ee7d31 100644 --- a/.github/workflows/go-c-cpp.yml +++ b/.github/workflows/go-c-cpp.yml @@ -38,7 +38,7 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v4 with: - args: --disable-all -E errcheck + args: --disable-all -E errcheck -E staticcheck skip-cache: true skip-pkg-cache: true skip-build-cache: true diff --git a/pkg/util/ebpf/bpf_test.go b/pkg/util/ebpf/bpf_test.go index 547af8fbc..c2ecc7ed7 100644 --- a/pkg/util/ebpf/bpf_test.go +++ b/pkg/util/ebpf/bpf_test.go @@ -36,7 +36,7 @@ func TestBpfConfig(t *testing.T) { configPaths = []string{ "/xxxxx/proc/config.gz", // android } - m, e := GetSystemConfig() + _, e := GetSystemConfig() if e != nil { // 正常情况 是没有找到配置文件 t.Logf("GetSystemConfig error:%s", e.Error()) @@ -47,7 +47,7 @@ func TestBpfConfig(t *testing.T) { configPaths = []string{ "config.gz", // test file from pixel 6 android 12 } - m, e = GetSystemConfig() + m, e := GetSystemConfig() if e != nil { t.Fatalf("GetSystemConfig(gzip) error:%s", e.Error()) } diff --git a/user/config/config_gotls.go b/user/config/config_gotls.go index 3ff7f4c72..3eab28e9a 100644 --- a/user/config/config_gotls.go +++ b/user/config/config_gotls.go @@ -98,7 +98,7 @@ func (gc *GoTLSConfig) Check() error { case "amd64": case "arm64": default: - err = fmt.Errorf("unsupport CPU arch :%s", goElfArch) + return fmt.Errorf("unsupport CPU arch :%s", goElfArch) } gc.goElfArch = goElfArch gc.goElf = goElf @@ -156,7 +156,7 @@ func (gc *GoTLSConfig) findRetOffsets(symbolName string) ([]int, error) { var offsets []int var instHex []byte instHex = elfText[start:end] - offsets, err = gc.decodeInstruction(instHex) + offsets, _ = gc.decodeInstruction(instHex) if len(offsets) == 0 { return offsets, ErrorNoRetFound } diff --git a/user/event/event_gotls.go b/user/event/event_gotls.go index 57a9c87b5..ae1392053 100644 --- a/user/event/event_gotls.go +++ b/user/event/event_gotls.go @@ -29,7 +29,9 @@ func (ge *GoTLSEvent) Decode(payload []byte) error { } if ge.Len > 0 { ge.Data = make([]byte, ge.Len) - err = binary.Read(r, binary.LittleEndian, &ge.Data) + if err = binary.Read(r, binary.LittleEndian, &ge.Data); err != nil { + return err + } } decodedKtime, err := DecodeKtime(int64(ge.TimestampNS), true) if err == nil { From 67a670cc68ba977d614d79da69904b589795de9d Mon Sep 17 00:00:00 2001 From: ruitianzhong Date: Thu, 29 Feb 2024 16:25:02 +0800 Subject: [PATCH 3/5] fix CI linter error Signed-off-by: ruitianzhong --- cli/cmd/gnutls.go | 9 ++++----- cli/cmd/gotls.go | 9 ++++----- cli/cmd/nss.go | 9 ++++----- cli/cmd/tls.go | 13 +++++-------- user/module/probe_pcap.go | 2 +- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/cli/cmd/gnutls.go b/cli/cmd/gnutls.go index 85eb3c0c4..2d3397eea 100644 --- a/cli/cmd/gnutls.go +++ b/cli/cmd/gnutls.go @@ -71,7 +71,7 @@ func gnuTlsCommandFunc(command *cobra.Command, args []string) { logger.Printf("ECAPTURE :: %s Version : %s", cliName, GitVersion) logger.Printf("ECAPTURE :: Pid Info : %d", os.Getpid()) var version kernel.Version - version, err = kernel.HostVersion() + version, _ = kernel.HostVersion() // it's safe to ignore error because we have checked it in func main logger.Printf("ECAPTURE :: Kernel Info : %s", version.String()) modNames := []string{module.ModuleNameGnutls} @@ -85,13 +85,12 @@ func gnuTlsCommandFunc(command *cobra.Command, args []string) { logger.Printf("ECAPTURE :: \tcant found module: %s", modName) break } - - var conf config.IConfig - conf = gc - if conf == nil { + if gc == nil { logger.Printf("ECAPTURE :: \tcant found module %s config info.", mod.Name()) break } + var conf config.IConfig + conf = gc conf.SetPid(gConf.Pid) conf.SetUid(gConf.Uid) diff --git a/cli/cmd/gotls.go b/cli/cmd/gotls.go index 1a9218614..37055725b 100644 --- a/cli/cmd/gotls.go +++ b/cli/cmd/gotls.go @@ -75,7 +75,7 @@ func goTLSCommandFunc(command *cobra.Command, args []string) { logger.Printf("ECAPTURE :: %s Version : %s", cliName, GitVersion) logger.Printf("ECAPTURE :: Pid Info : %d", os.Getpid()) var version kernel.Version - version, err = kernel.HostVersion() + version, _ = kernel.HostVersion() // it's safe to ignore error because we have checked it in func main logger.Printf("ECAPTURE :: Kernel Info : %s", version.String()) mod := module.GetModuleByName(module.ModuleNameGotls) @@ -83,13 +83,12 @@ func goTLSCommandFunc(command *cobra.Command, args []string) { logger.Printf("ECAPTURE :: \tcant found module: %s", module.ModuleNameGotls) return } - - var conf config.IConfig - conf = goc - if conf == nil { + if goc == nil { logger.Printf("ECAPTURE :: \tcant found module %s config info.", mod.Name()) return } + var conf config.IConfig + conf = goc conf.SetPid(gConf.Pid) conf.SetUid(gConf.Uid) diff --git a/cli/cmd/nss.go b/cli/cmd/nss.go index b8aeac332..70f3d8d2c 100644 --- a/cli/cmd/nss.go +++ b/cli/cmd/nss.go @@ -71,7 +71,7 @@ func nssCommandFunc(command *cobra.Command, args []string) { logger.Printf("ECAPTURE :: %s Version : %s", cliName, GitVersion) logger.Printf("ECAPTURE :: Pid Info : %d", os.Getpid()) var version kernel.Version - version, err = kernel.HostVersion() + version, _ = kernel.HostVersion() // it's safe to ignore error because we have checked it in func main logger.Printf("ECAPTURE :: Kernel Info : %s", version.String()) modNames := []string{module.ModuleNameNspr} @@ -85,13 +85,12 @@ func nssCommandFunc(command *cobra.Command, args []string) { logger.Printf("ECAPTURE :: \tcant found module: %s", modName) break } - - var conf config.IConfig - conf = nc - if conf == nil { + if nc == nil { logger.Printf("ECAPTURE :: \tcant found module %s config info.", mod.Name()) break } + var conf config.IConfig + conf = nc conf.SetPid(gConf.Pid) conf.SetUid(gConf.Uid) diff --git a/cli/cmd/tls.go b/cli/cmd/tls.go index c1e22f2fb..de6e0760b 100644 --- a/cli/cmd/tls.go +++ b/cli/cmd/tls.go @@ -85,10 +85,9 @@ func openSSLCommandFunc(command *cobra.Command, args []string) { logger.Printf("ECAPTURE :: %s Version : %s", cliName, GitVersion) logger.Printf("ECAPTURE :: Pid Info : %d", os.Getpid()) var version kernel.Version - version, err = kernel.HostVersion() + version, _ = kernel.HostVersion() // it's safe to ignore err because we have checked it in func main logger.Printf("ECAPTURE :: Kernel Info : %s", version.String()) - modNames := []string{} - modNames = []string{module.ModuleNameOpenssl} + modNames := []string{module.ModuleNameOpenssl} var runMods uint8 runModules := make(map[string]module.IModule) @@ -100,14 +99,12 @@ func openSSLCommandFunc(command *cobra.Command, args []string) { logger.Printf("ECAPTURE :: \tcant found module: %s", modName) break } - - var conf config.IConfig - conf = oc - - if conf == nil { + if oc == nil { logger.Printf("ECAPTURE :: \tcant found module %s config info.", mod.Name()) break } + var conf config.IConfig + conf = oc conf.SetPid(gConf.Pid) conf.SetUid(gConf.Uid) diff --git a/user/module/probe_pcap.go b/user/module/probe_pcap.go index 23233c7ba..6a87bd822 100644 --- a/user/module/probe_pcap.go +++ b/user/module/probe_pcap.go @@ -339,7 +339,7 @@ func prepareInsnPatchers(m *manager.Manager, ebpfFuncs []string, pcapFilter stri } for _, progSpec := range progSpecs { - progSpec, err = injectPcapFilter(progSpec, pcapFilter) + _, err = injectPcapFilter(progSpec, pcapFilter) if err != nil { return fmt.Errorf("failed to inject pcap filter for %s: %w", ebpfFunc, err) } From 48eac9d5265f071825c1791f747ee0cdba61927b Mon Sep 17 00:00:00 2001 From: ruitianzhong Date: Thu, 29 Feb 2024 16:32:26 +0800 Subject: [PATCH 4/5] safely remove empty branch Signed-off-by: ruitianzhong --- user/module/imodule.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/user/module/imodule.go b/user/module/imodule.go index 735207870..93af21fb6 100644 --- a/user/module/imodule.go +++ b/user/module/imodule.go @@ -85,10 +85,8 @@ func (m *Module) Init(ctx context.Context, logger *log.Logger, conf config.IConf m.logger = logger m.processor = event_processor.NewEventProcessor(logger, conf.GetHex()) m.isKernelLess5_2 = false //set false default - kv, err := kernel.HostVersion() - if err != nil { - // nothing to do. - } + kv, _ := kernel.HostVersion() + // it's safe to ignore err because we have checked it in main funcition if kv < kernel.VersionCode(5, 2, 0) { m.isKernelLess5_2 = true } From 1d160264f7f2636d54620e4db23afc75403b4daf Mon Sep 17 00:00:00 2001 From: ruitianzhong Date: Thu, 29 Feb 2024 18:06:38 +0800 Subject: [PATCH 5/5] add `staticcheck` for ubuntu-22.04 Signed-off-by: ruitianzhong --- .github/workflows/go-c-cpp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go-c-cpp.yml b/.github/workflows/go-c-cpp.yml index 301ee7d31..eeefd42b5 100644 --- a/.github/workflows/go-c-cpp.yml +++ b/.github/workflows/go-c-cpp.yml @@ -81,7 +81,7 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v4 with: - args: --disable-all -E errcheck + args: --disable-all -E errcheck -E staticcheck skip-cache: true skip-pkg-cache: true skip-build-cache: true