Here's what you'd have to do since the M5 is not a free-floating handguard rail.
First, you'd have to strip the barrel of the front sight tower.
Then you'd have to put a low-pro gasblock in the same place the front sight post was. Next, you'd have reinstall the same front sight post, but this time further up on the barrel (assuming you have room) because the M5 requires the handguard cap for installation (or so I remember).
Now assuming you get all that done, there is no guarantee the low profile gasblock will clear the M5, which means some filing may have to be done.
All of the above will also require the removale of any flash hider, and that means that if you have a 14.5" barrel with a pinned on FH you will need to have to do extensive work to get that off and will have to permanently attach it as well when you are done.
All said, Forest is right, try trading to an actual M4 rail.
In essence, what you are thinking of is converting your M4 into a BM Dissipator. But in the process pouring more money into it than buying a Dissipator upfront.