Erlang: binary_to_atom filling up atom table space security issue

萝らか妹 提交于 2019-12-05 16:42:40

When an atom is first used it is given an internal number and put in an array in the VM. This array is allocated statically and can fill up if enough different atoms are used. binary_to_existing_atom will only convert a binary string to an atom which already exists in the array, if it does not exist the call will fail.

If you are converting input data directly to atoms without doing any sanity checks it would be possible for an external client to send <<"a">> and <<"b">> until the array is full at which point the vm will crash.

Another way to avoid this is to simply not use binary_to_atom and instead pattern match on different binaries and return the desired atom.

list_to_atom/1 and binary_to_atom/1 are very serious bugs in erlang code. Always create a major function like this:

to_atom(X) when is_list(X) -> 
  try list_to_existing_atom(X) of
     Atom -> Atom
  catch
    _Error:_ErrorReason -> list_to_atom(X)
  end.
In this way, if the atom already exists in the Atom table, the try body avoids creating the atom again. Its only created the first time this function is called.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!