0

We all have (or know someone who has) unintentionally misused the destroy-disk (dd) command. What ways exist (if any) to change the command in the following or a similar way: If /dev/sda is given as the output file (of=/dev/sda) the command doesn't run or prompts for confirmation with something like "are you sure about that"?

Could you achieve something like that using your .bashrc file?

Is there a way, in general, to stop certain commands from running when certain arguments are passed?

Edit: The command is run as root.

  • 1
    You can create a wrapper and put it in your $PATH. – Arkadiusz Drabczyk Apr 23 '18 at 18:36
  • 7
    they will just invent a better idiot who will find some new way to turn the power tool dd into a footgun – thrig Apr 23 '18 at 18:37
  • 5
    There is no way to prevent people from screwing things up. Don't give user access to login as root, and don't put them in the 'disk' group? Just have good backup system so you can recover things when people make errors? Any attempt to filter things will probably break commands/scripts that use dd. – Zoredache Apr 23 '18 at 18:41
  • 2
    Make something idiot proof, and some moron will come along with a better idiot. – DopeGhoti Apr 23 '18 at 18:45
  • @ArkadiuszDrabczyk feel free to elaborate on wrappers for commands in an answer! – technical_difficulty Apr 23 '18 at 18:49
4

As Arkadiusz said, you can create a wrapper:

dd() {
  # Limit variables' scope
  local args command output reply

  # Basic arguments handling
  while (( ${#} > 0 )); do
    case "${1}" in
    ( of=* )
      output="${1#*=}"
      ;;
    ( * )
      args+=( "${1}" )
      ;;
    esac
    shift || break
  done

  # Build the actual command
  command=( command -- dd "${args[@]}" "of=${output}" )

  # Warn the user
  printf 'Please double-check this to avoid potentially dangerous behavior.\n' >&2
  printf 'Output file: %s\n' "${output}" >&2

  # Ask for confirmation
  IFS= read -p 'Do you want to continue? (y/n): ' -r reply

  # Check user's reply
  case "${reply}" in
  ( y | yes )
    printf 'Running command...\n' >&2
    ;;
  ( * )
    printf 'Aborting\n' >&2
    return
    ;;
  esac

  # Run command
  "${command[@]}"
}

Example:

$ dd if=/dev/urandom of=file.txt bs=4M count=5
Please double-check this to avoid potentially dangerous behavior.
Output file: file.txt
Do you want to continue? (y/n): y
Running command...
5+0 records in
5+0 records out
20971520 bytes (21 MB, 20 MiB) copied, 0.443037 s, 47.3 MB/s

Modify it to fit your needs (make it POSIX-compliant, check for other conditions, etc).

| improve this answer | |
3

AFAIK, a non-superuser cannot overwrite /dev/sda. If you can't trust your operators who have been entrusted with superuser access, you have larger problems than /bin/dd.

| improve this answer | |
  • 1
    The assumption is that the command is run as root – technical_difficulty Apr 23 '18 at 18:42
  • 6
    Firstly, the assumption was not made in the question. Please edit the question to clarify. Secondly, the problem still exists. There are myriad ways for a superuser to destroy a system with tools not meant to be so destructive. I could readily come up with ten examples. You need to fix your users, not the tools at their disposal. – DopeGhoti Apr 23 '18 at 18:45
  • 1
    The havoc you can make with a simple rm -rf /some/important/directory (read man rm!!)... ^_^; – Mio Rin Apr 25 '18 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.