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

Kshitij Gupta kgupta8592 at gmail.com
Fri Oct 10 17:46:14 UTC 2014


Hello,

On Fri, Oct 10, 2014 at 11:03 PM, Christian Boltz <apparmor at cboltz.de> wrote:
> 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 ;-)
>

This has been a long-standing problem, thanks for looking it up.

>
> === 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'
> +

I think we can move this redundant version check part to module level
instead of function level.

>      try:
> -        orig = codecs.open(path, 'w', 'UTF-8')
> +        orig = codecs.open(path, 'w', 'UTF-8', errors=errorhandling)
>      except Exception:
>          raise
>      return orig
>
>

Thanks for the patch.

With the suggested change.

Acked-by: Kshitij Gupta <kgupta8592 at gmail.com>.

Regards,
Kshitij Gupta

>
> 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]
>
>
> --
> AppArmor mailing list
> AppArmor at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/apparmor



More information about the AppArmor mailing list