how to run setuid task properly?

Putnik

Assuming there is a php website and I want to block an ip at the firewall level based on the site code execution. The site is run under non-root user.

I was going to pass IP from the site code to a script (writeable to root only) like

#!/bin/bash
function validate_ip()
{ ... code here ...}
if validate_ip $1; then 
    /usr/sbin/iptables -I INPUT -s $1 -j DROP
    echo 'blocked'; 
else 
    echo 'bad IP $1'; 
fi

using suid bit. I want to add additional IP validation to avoid XSS and other bad things (consider it paranoia if you like), so do not want to allow the site to call iptables directly.

The script does not work can't initialize iptables table 'filter': Permission denied (you must be root) because bash drops suid bit

There is workaround: allow iptables in sudo but I don't think it's secure. I have no time/possibility to develop/buy a binary which will do the task. One suggested binary wrapper around script but I hesitate, perhaps there is a better way?

So, the question is: how can I allow non-root app to block ip in iptables firewall in a secure way?

derobert

Instead of making your bash script suid root, run your bash script through sudo. As a side benefit, this also lets you easily lock down who can run your script as root and also the arguments passed. You could, for example, only allow:

phpuser ALL=(root) NOPASSWD: /usr/local/sbin/your-script [0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9]

then make sure your PHP script always formats each IP address octet as three digits.

If it's too hard to have PHP call sudo (which it shouldn't be!) you can have the script do it itself, with something like:

#!/bin/sh

[ "$(id -u)" -eq 0 ] || exec sudo -- "$0" "$@"
# rest of script here

(I'm not entirely sure iptables will be happy with the leading 0s, if not you can strip them off).

PS: Please quote your variables in your shell script:

if validate_ip "$1"; then 
    /usr/sbin/iptables -I INPUT -s "$1" -j DROP
    # ⋮

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Multiple Task.Run not waiting/running properly

分類Dev

How do I run a mix task from within a mix task?

分類Dev

How to stop a Airflow DAG run from task

分類Dev

How to use windows task scheduler to run updatedb?

分類Dev

Callable Task is not executing properly

分類Dev

How do you run a setup.py file properly?

分類Dev

How to make mongo properly run my javascript script

分類Dev

How do I run "srb tc --lsp" properly?

分類Dev

How to run a task ONLY on modified file with Gulp watch

分類Dev

How to only allow a specific machine to run a task in Luigi

分類Dev

How to unacknowledge message when I run a celery task?

分類Dev

How to run db:migrate from another rake task with parameters?

分類Dev

How do I spawn a task that will run to completion and immediately return to the client?

分類Dev

SCHTASKS - How to Create Scheduled Task With Max Run Time (Without Interval)

分類Dev

How to design a task scheduler in nodejs with rabbitmq that won't run the same task when spanning multiple process?

分類Dev

Ansible: run a failed task

分類Dev

Task.FromResult()とTask.Run()

分類Dev

public static Task Run(Func <Task> function);

分類Dev

Calling await on a task created with Task.Run()

分類Dev

Windows Task Scheduler, run task if task isn't running?

分類Dev

Cronjob won't run properly

分類Dev

Task.Run( () MethodName()) and await Task.Run(async () => MethodName())

分類Dev

Task.Run with Parameter(s)?

分類Dev

Task.Run in Static Initializer

分類Dev

Task.Runで動的

分類Dev

Javafx - Execution failed for task ':run'

分類Dev

Force run of a task skipped by ShortCircuitOperator

分類Dev

Task.Runの代替

分類Dev

How do I Properly run NPM Install in Visual Studio Team Services Build Agent

Related 関連記事

  1. 1

    Multiple Task.Run not waiting/running properly

  2. 2

    How do I run a mix task from within a mix task?

  3. 3

    How to stop a Airflow DAG run from task

  4. 4

    How to use windows task scheduler to run updatedb?

  5. 5

    Callable Task is not executing properly

  6. 6

    How do you run a setup.py file properly?

  7. 7

    How to make mongo properly run my javascript script

  8. 8

    How do I run "srb tc --lsp" properly?

  9. 9

    How to run a task ONLY on modified file with Gulp watch

  10. 10

    How to only allow a specific machine to run a task in Luigi

  11. 11

    How to unacknowledge message when I run a celery task?

  12. 12

    How to run db:migrate from another rake task with parameters?

  13. 13

    How do I spawn a task that will run to completion and immediately return to the client?

  14. 14

    SCHTASKS - How to Create Scheduled Task With Max Run Time (Without Interval)

  15. 15

    How to design a task scheduler in nodejs with rabbitmq that won't run the same task when spanning multiple process?

  16. 16

    Ansible: run a failed task

  17. 17

    Task.FromResult()とTask.Run()

  18. 18

    public static Task Run(Func <Task> function);

  19. 19

    Calling await on a task created with Task.Run()

  20. 20

    Windows Task Scheduler, run task if task isn't running?

  21. 21

    Cronjob won't run properly

  22. 22

    Task.Run( () MethodName()) and await Task.Run(async () => MethodName())

  23. 23

    Task.Run with Parameter(s)?

  24. 24

    Task.Run in Static Initializer

  25. 25

    Task.Runで動的

  26. 26

    Javafx - Execution failed for task ':run'

  27. 27

    Force run of a task skipped by ShortCircuitOperator

  28. 28

    Task.Runの代替

  29. 29

    How do I Properly run NPM Install in Visual Studio Team Services Build Agent

ホットタグ

アーカイブ