ValidatePID performs comprehensive PID validation according to security requirements
(pid int)
| 42 | |
| 43 | // ValidatePID performs comprehensive PID validation according to security requirements |
| 44 | func (pv *ProcessValidator) ValidatePID(pid int) error { |
| 45 | // Check PID is a positive integer and within valid range |
| 46 | if pid <= 0 { |
| 47 | return PIDValidationError{ |
| 48 | PID: pid, |
| 49 | Message: "PID must be a positive integer", |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Check maximum PID based on OS |
| 54 | maxPID := pv.getMaxPID() |
| 55 | if pid > maxPID { |
| 56 | return PIDValidationError{ |
| 57 | PID: pid, |
| 58 | Message: fmt.Sprintf("PID exceeds maximum allowed value (%d)", maxPID), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Safety check: prevent killing critical system processes |
| 63 | if pv.isCriticalSystemProcess(pid) { |
| 64 | return PIDValidationError{ |
| 65 | PID: pid, |
| 66 | Message: "Cannot kill critical system process", |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Verify process exists and is not a kernel thread |
| 71 | if err := pv.verifyProcessExists(pid); err != nil { |
| 72 | return PIDValidationError{ |
| 73 | PID: pid, |
| 74 | Message: err.Error(), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Check if process is a kernel thread (Linux-specific) |
| 79 | if runtime.GOOS == "linux" { |
| 80 | if isKernel, err := pv.isKernelThread(pid); err != nil { |
| 81 | return PIDValidationError{ |
| 82 | PID: pid, |
| 83 | Message: fmt.Sprintf("Error checking process type: %v", err), |
| 84 | } |
| 85 | } else if isKernel { |
| 86 | return PIDValidationError{ |
| 87 | PID: pid, |
| 88 | Message: "Cannot kill kernel thread", |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | // ValidatePIDWithOwnership performs comprehensive PID validation including process ownership checks |
| 97 | func (pv *ProcessValidator) ValidatePIDWithOwnership(pid int) error { |
no test coverage detected