summaryrefslogtreecommitdiffstats
path: root/utilities/mpostcheck.sh
blob: 9d4b939e6122cd978b69ab2a1281950f9347217b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
# Check the presence of "mpost" in LaTeX sources
#
# A POC of an arbitrary code execution vulnerability in the default
# configuration of TeX packages was disclosed at
# https://scumjr.github.io/2016/11/28/pwning-coworkers-thanks-to-latex/.
# TeX Live 2016 is updated on November 30, 2016 to plug the security hole
# by removing "mpost" from the "shell_escape_commands" variable of default
# texmf configuration.
# However, depending on the customization of a user, he/she can still be
# affected after the update.
#
# To prevent exploitation of the vulnerability, this script checks
# if "mpost" is present in source files of perfbook.
# If the vulnerability is fixed in your TeX environment, the check is
# skipped.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
#
# Copyright (C) Akira Yokosawa, 2016, 2019
#
# Authors: Akira Yokosawa <akiyks@gmail.com>
#
#-------------------------------------------------------------------
# Instruction to plug the security hole
# (based on https://zrbabbler.hatenablog.com/entry/20161206/1481039449
# (in Japanese), translated and supplemented by Akira Yokosawa)
#
# 1. Test the config of your TeX environment
#
# Enter the following in a command shell:
#
#   $ kpsewhich -var-value=shell_escape_commands
#
# Example output:
#
#   bibtex,bibtex8,extractbb,kpsewhich,makeindex,mpost,repstopdf
#
# If "mpost" appears in the output, your setting is vulnerable.
# Following variants of "mpost" are also vulnerable:
#    pmpost
#    jmpost
#    upmpost
#
# Note:
#   "rmpost", "rpmpost", "r-mpost", "r-pmpost", and "r-upmpost"
#    in the list are known to be safe.
#
# 2. Solution
#
# 2-1. Update TeX distribution if possible
#
#   However, depending on your customization, you may still be vulnerable.
#   Do Step 1 again after the update.
#   If you are still vulnerable, proceed to Step 2-2.
#
# 2-2. Modify texmf configuration
#
# 2-2-1. Using tlmgr
#
#   If tlmgr is available, enter the following command in a command shell:
#
#   $ tlmgr conf texmf shell_escape_commands [list]
#
#   Here, [list] is a command list displayed in Step 1 with "mpost," removed,
#   e.g.:
#
#   $ tlmgr conf texmf shell_escape_commands \
#   > bibtex,bibtex8,extractbb,kpsewhich,makeindex,repstopdf
#
# 2-2-2. Manual fix
#
#  If tlmgr is not available, proceed as follows:
#
#  o Search effective texmf.cnf
#
#   Enter the following command:
#
#   $ kpsewhich texmf.cnf
#
#   The path displayed is the effective one.
#
#  o Edit the texmf.cnf to remove "mpost" from shell_escape_commands
#
#   If there is a line beginning with "shell_escape_commands=" in the
#   texmf.cnf file, edit it to remove "mpost,".
#
#   If there is not such a line, add a line of:
#
#   shell_escape_commands=[list]
#
#   where [list] is again a command list displayed in Step 1 with "mpost,"
#   removed, e.g.:
#
#   shell_escape_commands=bibtex,bibtex8,extractbb,kpsewhich,makeindex,repstopdf
#
# Note:
#   If the effective texmf.cnf has a comment saying not to edit it directly,
#   follow the instruction given there.
#-------------------------------------------------------------------

: ${WHICH:=command -v}

dogrep() {
	texsrc=`find . -name "*.tex" -print`
	bibsrc=`find . -name "*.bib" -print`
	stysrc=`find . -name "*.sty" -print`
	clssrc=`find . -name "*.cls" -print`
	bstsrc=`find . -name "*.bst" -print`
	perfbooksrc="$texsrc $bibsrc $stysrc $clssrc $bstsrc"
	if grep -w -n "mpost" $perfbooksrc || \
			grep -w -n "[jp]mpost" $perfbooksrc || \
			grep -w -n "upmpost" $perfbooksrc
	then
		echo "#####################################################"
		echo "## 'mpost' is found in LaTeX sources. Aborting...  ##"
		echo "## Refer to comment in utilities/mpostcheck.sh.    ##"
		echo "#####################################################"
		exit 1
	fi
}

if $WHICH kpsewhich >/dev/null
then
	command_list_orig=`kpsewhich -var-value=shell_escape_commands`
	command_list=`echo $command_list_orig | sed -E "s/r-u?p?mpost,//g"`
	if echo $command_list  | grep -w -q "mpost" || \
			echo $command_list | grep -w -q "[jp]mpost" || \
			echo $command_list | grep -w -q "upmpost"
	then
		echo "kpsewhich -var-value=shell_escape_commands"
		echo $command_list_orig
		echo "WARNING: Refer to utilities/mpostcheck.sh for texmf config fix."
		dogrep
	else
		exit 0
	fi
else
	dogrep
	exit 0
fi