XPrivacyLua is a module for Xposed framework which does exactly what you need. It is free and open source. Works on rooted devices. It's the successor of XPrivacy.
Install Xposed from here: https://forum.xda-developers.com/showthread.php?t=3034811
You can then download the XPrivacyLua module from the Xposed repo through the Xposed Manager app, or manually from here:
https://repo.xposed.info/module/eu.faircode.xlua
Source:
https://github.com/M66B/XPrivacyLua
If you're running Android 5 or lower, you can use the legacy XPrivacy module.