[apparmor] [patch] prevent encoding errors when reading a file

Christian Boltz apparmor at cboltz.de
Fri Oct 10 17:33:04 UTC 2014


Hello,

the following patch changes open_file_read() and open_file_write() to 
use errors='surrogateescape' (with fallback to 'replace' for py2).

This avoids a crash when reading a logfile with special characters that 
are not utf8-encoded (for example a latin1 "ö"), and also avoids crashes 
at several other places we don't know yet ;-)


=== modified file 'utils/apparmor/common.py'
--- utils/apparmor/common.py    2014-02-28 22:31:16 +0000
+++ utils/apparmor/common.py    2014-10-10 17:31:39 +0000
@@ -168,8 +168,13 @@
 
 def open_file_read(path, encoding='UTF-8'):
     '''Open specified file read-only'''
+
+    errorhandling = 'surrogateescape'
+    if sys.version_info[0] < 3:
+        errorhandling = 'replace'
+
     try:
-        orig = codecs.open(path, 'r', encoding)
+        orig = codecs.open(path, 'r', encoding, errors=errorhandling)
     except Exception:
         raise
 
@@ -177,8 +182,13 @@
 
 def open_file_write(path):
     '''Open specified file in write/overwrite mode'''
+
+    errorhandling = 'surrogateescape'
+    if sys.version_info[0] < 3:
+        errorhandling = 'replace'
+
     try:
-        orig = codecs.open(path, 'w', 'UTF-8')
+        orig = codecs.open(path, 'w', 'UTF-8', errors=errorhandling)
     except Exception:
         raise
     return orig



Regards,

Christian Boltz
-- 
Auch ich rate von Sandpapier dringend ab! Ein Fehler, der, besonders von
Anfängern, immer wieder gemacht wird! Ein Spritzer Pril auf 1/2 Tasse
Java Kaffee und damit spülen - das ist IMO wesentlich schonender.
[Olaf Andersen erklärt das Putzen einer Festplatte in suse-linux]




More information about the AppArmor mailing list