Even if you're reporting hours accurately and either of the companies finds out about it, then you'll probably be asked to leave regardless. This is true also if you're salaried.
If it's a government subcontract, you could end up going to jail for this.
Realistically the way to do this without problems would be to start your own consulting firm, and bid on work. Then you can work on two projects at once (as long as it's not hourly, something like a firm fixed price contract). You're basically on the hook for delivering it. The customer is not paying for your time, just the product you're delivering.
Edited to add:
In the salaried world you have a conflict of interest. You may choose a worse technique or technology for company A if it means you won't be able to give company B enough time. Or you might put in shittier code than needed to be created because you had to save time for company B.
Company A would say those extra hours could have been put to use for more testing, more robust coding, more robust unit testing.
Worse would be if you stole code that solved a problem for Company A and gave it to Company B and then took credit for it.
So it would be in the best interest for Company A and Company B to let you go if either found out about it.