ocaml-opasswd we are using ctypes to bind some functions from
shadow.h. In rare cases, when using the library to update an entry in the shadow file, it seems that the two strings in the passwd struct are read from uninitialised memory instead of being copies of the ocaml strings that we passed to create the new shadow struct.
The code is here: https://github.com/xapi-project/ocaml-opasswd/blob/master/lib/shadow.ml#L20 and my fear is that declaring those as
let sp_name = field shadow_t "sp_name" string let sp_passwd = field shadow_t "sp_passwd" string
and then reading or writing them with
name = getf !@sp sp_name; passwd = getf !@sp sp_passwd;
setf sp_t sp_name sp.name; setf sp_t sp_passwd sp.passwd;
could be wrong. Reading the documentation, it seems that strings view are doing the right copies and allocation there but I don’t see other major points of failure. This code has been there for ages, and if it is wrong I am not sure how it should be fixed.
Is that clearly wrong? Ho should I declare those strings and properly copy them between c and ocaml when using ctypes?
UPDATE: To add some more context, the part of the code that seems to behave badly (sometimes, rarely) is https://github.com/xapi-project/ocaml-opasswd/blob/master/lib/common.ml#L19-L21