Permanently shaded areas near the poles of the Moon and Mercury may harbor water ice. We develop a physical model for migration of water molecules in the regolith and discover two pathways that can lead to accumulation of H2O in the subsurface. A small fraction of water molecules delivered, either continuously or abruptly, to permanently cold areas diffuses into the regolith and can remain there longer than on the surface. Higher temperatures lead to deeper burial. At constant temperature, this diffusive migration produces less than one molecular layer of volatile H2O on grains, because it is driven by differences in surface concentrations. The water is therefore expected to be in adsorbed form, and the amount stored in this fashion could be at most a few hundred ppm of H2O. A second pathway is pumping by diurnal temperature oscillations from a transient ice cover that may have formed during a large comet impact. It can lead to high ground ice densities, but the ground ice layer lasts not long beyond the disappearance of the ice cover. Both types of subsurface charging mechanism work best for temperatures typical of permanently shaded areas with sunlit surfaces in their field of view.