I did a search on this and did not find anything so I thought I would suggest a way to protect CubeMX generated code that has been user modified in areas that are not specifically protected from CubeMX updates such as /* Begin User Code*/. I found this useful when making changes for SD micro card code generated by CubeMX. You can make a copy of the source file you want to change and name it something like bsp_driver_sd_fixed.c. Make the changes you need and then right click on the original bsp_driver_sd.c and exclude it from the build. In Eclipse based IDEs this would be a right click on the file and select Resource Configurations > Exclude from Build. In this way, if you have to regen the code in CubeMX the file that is excluded from the build is updated which has no effect on your code and your fixed file is left untouched. Of course, if the change you need is in the excluded file you can pick that up by doing a compare and migrating the changes as necessary.
Hope this helps.