mirror of
https://github.com/safing/portmaster
synced 2025-09-04 11:39:29 +00:00
Start process adaption
This commit is contained in:
parent
5533203fa1
commit
97a46d1e57
6 changed files with 89 additions and 167 deletions
|
@ -3,11 +3,11 @@
|
||||||
package process
|
package process
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Safing/safing-core/database"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
datastore "github.com/ipfs/go-datastore"
|
"github.com/Safing/portbase/database/record"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExecutableSignature stores a signature of an executable.
|
// ExecutableSignature stores a signature of an executable.
|
||||||
|
@ -15,52 +15,15 @@ type ExecutableSignature []byte
|
||||||
|
|
||||||
// FileInfo stores (security) information about a file.
|
// FileInfo stores (security) information about a file.
|
||||||
type FileInfo struct {
|
type FileInfo struct {
|
||||||
database.Base
|
record.Base
|
||||||
|
sync.Mutex
|
||||||
|
|
||||||
HumanName string
|
HumanName string
|
||||||
Owners []string
|
Owners []string
|
||||||
ApproxLastSeen int64
|
ApproxLastSeen int64
|
||||||
Signature *ExecutableSignature
|
Signature *ExecutableSignature
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileInfoModel *FileInfo // only use this as parameter for database.EnsureModel-like functions
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
database.RegisterModel(fileInfoModel, func() database.Model { return new(FileInfo) })
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create saves FileInfo with the provided name in the default namespace.
|
|
||||||
func (m *FileInfo) Create(name string) error {
|
|
||||||
return m.CreateObject(&database.FileInfoCache, name, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateInNamespace saves FileInfo with the provided name in the provided namespace.
|
|
||||||
func (m *FileInfo) CreateInNamespace(namespace *datastore.Key, name string) error {
|
|
||||||
return m.CreateObject(namespace, name, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save saves FileInfo.
|
|
||||||
func (m *FileInfo) Save() error {
|
|
||||||
return m.SaveObject(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// getFileInfo fetches FileInfo with the provided name from the default namespace.
|
|
||||||
func getFileInfo(name string) (*FileInfo, error) {
|
|
||||||
return getFileInfoFromNamespace(&database.FileInfoCache, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// getFileInfoFromNamespace fetches FileInfo with the provided name from the provided namespace.
|
|
||||||
func getFileInfoFromNamespace(namespace *datastore.Key, name string) (*FileInfo, error) {
|
|
||||||
object, err := database.GetAndEnsureModel(namespace, name, fileInfoModel)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
model, ok := object.(*FileInfo)
|
|
||||||
if !ok {
|
|
||||||
return nil, database.NewMismatchError(object, fileInfoModel)
|
|
||||||
}
|
|
||||||
return model, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFileInfo gathers information about a file and returns *FileInfo
|
// GetFileInfo gathers information about a file and returns *FileInfo
|
||||||
func GetFileInfo(path string) *FileInfo {
|
func GetFileInfo(path string) *FileInfo {
|
||||||
// TODO: actually get file information
|
// TODO: actually get file information
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Safing/safing-core/network/packet"
|
"github.com/Safing/portmaster/network/packet"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package process
|
package process
|
||||||
|
|
||||||
import "github.com/Safing/safing-core/process/proc"
|
import "github.com/Safing/portmaster/process/proc"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
getTCP4PacketInfo = proc.GetTCP4PacketInfo
|
getTCP4PacketInfo = proc.GetTCP4PacketInfo
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/Safing/safing-core/log"
|
"github.com/Safing/portbase/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/Safing/safing-core/log"
|
"github.com/Safing/portbase/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -3,22 +3,23 @@
|
||||||
package process
|
package process
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"sync"
|
||||||
|
|
||||||
datastore "github.com/ipfs/go-datastore"
|
|
||||||
processInfo "github.com/shirou/gopsutil/process"
|
processInfo "github.com/shirou/gopsutil/process"
|
||||||
|
|
||||||
"github.com/Safing/safing-core/database"
|
"github.com/Safing/portbase/database/record"
|
||||||
"github.com/Safing/safing-core/log"
|
"github.com/Safing/portbase/log"
|
||||||
"github.com/Safing/safing-core/profiles"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Process represents a process running on the operating system
|
// A Process represents a process running on the operating system
|
||||||
type Process struct {
|
type Process struct {
|
||||||
database.Base
|
record.Base
|
||||||
|
sync.Mutex
|
||||||
|
|
||||||
UserID int
|
UserID int
|
||||||
UserName string
|
UserName string
|
||||||
UserHome string
|
UserHome string
|
||||||
|
@ -30,58 +31,20 @@ type Process struct {
|
||||||
CmdLine string
|
CmdLine string
|
||||||
FirstArg string
|
FirstArg string
|
||||||
ProfileKey string
|
ProfileKey string
|
||||||
Profile *profiles.Profile
|
|
||||||
Name string
|
Name string
|
||||||
Icon string
|
Icon string
|
||||||
// Icon is a path to the icon and is either prefixed "f:" for filepath, "d:" for database cache path or "c:"/"a:" for a the icon key to fetch it from a company / authoritative node and cache it in its own cache.
|
// Icon is a path to the icon and is either prefixed "f:" for filepath, "d:" for database cache path or "c:"/"a:" for a the icon key to fetch it from a company / authoritative node and cache it in its own cache.
|
||||||
}
|
}
|
||||||
|
|
||||||
var processModel *Process // only use this as parameter for database.EnsureModel-like functions
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
database.RegisterModel(processModel, func() database.Model { return new(Process) })
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create saves Process with the provided name in the default namespace.
|
|
||||||
func (m *Process) Create(name string) error {
|
|
||||||
return m.CreateObject(&database.Processes, name, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateInNamespace saves Process with the provided name in the provided namespace.
|
|
||||||
func (m *Process) CreateInNamespace(namespace *datastore.Key, name string) error {
|
|
||||||
return m.CreateObject(namespace, name, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save saves Process.
|
|
||||||
func (m *Process) Save() error {
|
|
||||||
return m.SaveObject(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetProcess fetches Process with the provided name from the default namespace.
|
// GetProcess fetches Process with the provided name from the default namespace.
|
||||||
func GetProcess(name string) (*Process, error) {
|
func GetProcess(name string) (*Process, error) {
|
||||||
return GetProcessFromNamespace(&database.Processes, name)
|
return nil, errors.New("NIY")
|
||||||
}
|
|
||||||
|
|
||||||
// GetProcessFromNamespace fetches Process with the provided name from the provided namespace.
|
|
||||||
func GetProcessFromNamespace(namespace *datastore.Key, name string) (*Process, error) {
|
|
||||||
object, err := database.GetAndEnsureModel(namespace, name, processModel)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
model, ok := object.(*Process)
|
|
||||||
if !ok {
|
|
||||||
return nil, database.NewMismatchError(object, processModel)
|
|
||||||
}
|
|
||||||
return model, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Process) String() string {
|
func (m *Process) String() string {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return "?"
|
return "?"
|
||||||
}
|
}
|
||||||
if m.Profile != nil && !m.Profile.Default {
|
|
||||||
return fmt.Sprintf("%s:%s:%d", m.UserName, m.Profile, m.Pid)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s:%s:%d", m.UserName, m.Path, m.Pid)
|
return fmt.Sprintf("%s:%s:%d", m.UserName, m.Path, m.Pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,10 +62,6 @@ func GetOrFindProcess(pid int) (*Process, error) {
|
||||||
case (pid == 0 && runtime.GOOS == "linux") || (pid == 4 && runtime.GOOS == "windows"):
|
case (pid == 0 && runtime.GOOS == "linux") || (pid == 4 && runtime.GOOS == "windows"):
|
||||||
new.UserName = "Kernel"
|
new.UserName = "Kernel"
|
||||||
new.Name = "Operating System"
|
new.Name = "Operating System"
|
||||||
new.Profile = &profiles.Profile{
|
|
||||||
Name: "OS",
|
|
||||||
Flags: []int8{profiles.Internet, profiles.LocalNet, profiles.Directconnect, profiles.Service},
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
pInfo, err := processInfo.NewProcess(int32(pid))
|
pInfo, err := processInfo.NewProcess(int32(pid))
|
||||||
|
@ -167,77 +126,77 @@ func GetOrFindProcess(pid int) (*Process, error) {
|
||||||
// new.Icon, err =
|
// new.Icon, err =
|
||||||
|
|
||||||
// get Profile
|
// get Profile
|
||||||
processPath := new.Path
|
// processPath := new.Path
|
||||||
var applyProfile *profiles.Profile
|
// var applyProfile *profiles.Profile
|
||||||
iterations := 0
|
// iterations := 0
|
||||||
for applyProfile == nil {
|
// for applyProfile == nil {
|
||||||
|
//
|
||||||
iterations++
|
// iterations++
|
||||||
if iterations > 10 {
|
// if iterations > 10 {
|
||||||
log.Warningf("process: got into loop while getting profile for %s", new)
|
// log.Warningf("process: got into loop while getting profile for %s", new)
|
||||||
break
|
// break
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
applyProfile, err = profiles.GetActiveProfileByPath(processPath)
|
// applyProfile, err = profiles.GetActiveProfileByPath(processPath)
|
||||||
if err == database.ErrNotFound {
|
// if err == database.ErrNotFound {
|
||||||
applyProfile, err = profiles.FindProfileByPath(processPath, new.UserHome)
|
// applyProfile, err = profiles.FindProfileByPath(processPath, new.UserHome)
|
||||||
}
|
// }
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Warningf("process: could not get profile for %s: %s", new, err)
|
// log.Warningf("process: could not get profile for %s: %s", new, err)
|
||||||
} else if applyProfile == nil {
|
// } else if applyProfile == nil {
|
||||||
log.Warningf("process: no default profile found for %s", new)
|
// log.Warningf("process: no default profile found for %s", new)
|
||||||
} else {
|
// } else {
|
||||||
|
//
|
||||||
// TODO: there is a lot of undefined behaviour if chaining framework profiles
|
// // TODO: there is a lot of undefined behaviour if chaining framework profiles
|
||||||
|
//
|
||||||
// process framework
|
// // process framework
|
||||||
if applyProfile.Framework != nil {
|
// if applyProfile.Framework != nil {
|
||||||
if applyProfile.Framework.FindParent > 0 {
|
// if applyProfile.Framework.FindParent > 0 {
|
||||||
var ppid int32
|
// var ppid int32
|
||||||
for i := uint8(1); i < applyProfile.Framework.FindParent; i++ {
|
// for i := uint8(1); i < applyProfile.Framework.FindParent; i++ {
|
||||||
parent, err := pInfo.Parent()
|
// parent, err := pInfo.Parent()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, err
|
// return nil, err
|
||||||
}
|
// }
|
||||||
ppid = parent.Pid
|
// ppid = parent.Pid
|
||||||
}
|
// }
|
||||||
if applyProfile.Framework.MergeWithParent {
|
// if applyProfile.Framework.MergeWithParent {
|
||||||
return GetOrFindProcess(int(ppid))
|
// return GetOrFindProcess(int(ppid))
|
||||||
}
|
// }
|
||||||
// processPath, err = os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
|
// // processPath, err = os.Readlink(fmt.Sprintf("/proc/%d/exe", pid))
|
||||||
// if err != nil {
|
// // if err != nil {
|
||||||
// return nil, fmt.Errorf("could not read /proc/%d/exe: %s", pid, err)
|
// // return nil, fmt.Errorf("could not read /proc/%d/exe: %s", pid, err)
|
||||||
// }
|
// // }
|
||||||
continue
|
// continue
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
newCommand, err := applyProfile.Framework.GetNewPath(new.CmdLine, new.Cwd)
|
// newCommand, err := applyProfile.Framework.GetNewPath(new.CmdLine, new.Cwd)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, err
|
// return nil, err
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// assign
|
// // assign
|
||||||
new.CmdLine = newCommand
|
// new.CmdLine = newCommand
|
||||||
new.Path = strings.SplitN(newCommand, " ", 2)[0]
|
// new.Path = strings.SplitN(newCommand, " ", 2)[0]
|
||||||
processPath = new.Path
|
// processPath = new.Path
|
||||||
|
//
|
||||||
// make sure we loop
|
// // make sure we loop
|
||||||
applyProfile = nil
|
// applyProfile = nil
|
||||||
continue
|
// continue
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// apply profile to process
|
// // apply profile to process
|
||||||
log.Debugf("process: applied profile to %s: %s", new, applyProfile)
|
// log.Debugf("process: applied profile to %s: %s", new, applyProfile)
|
||||||
new.Profile = applyProfile
|
// new.Profile = applyProfile
|
||||||
new.ProfileKey = applyProfile.GetKey().String()
|
// new.ProfileKey = applyProfile.GetKey().String()
|
||||||
|
//
|
||||||
// update Profile with Process icon if Profile does not have one
|
// // update Profile with Process icon if Profile does not have one
|
||||||
if !new.Profile.Default && new.Icon != "" && new.Profile.Icon == "" {
|
// if !new.Profile.Default && new.Icon != "" && new.Profile.Icon == "" {
|
||||||
new.Profile.Icon = new.Icon
|
// new.Profile.Icon = new.Icon
|
||||||
new.Profile.Save()
|
// new.Profile.Save()
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// get FileInfo
|
// get FileInfo
|
||||||
new.FileInfo = GetFileInfo(new.Path)
|
new.FileInfo = GetFileInfo(new.Path)
|
||||||
|
@ -245,7 +204,7 @@ func GetOrFindProcess(pid int) (*Process, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// save to DB
|
// save to DB
|
||||||
new.Create(strconv.Itoa(new.Pid))
|
// new.Save()
|
||||||
|
|
||||||
return new, nil
|
return new, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue