These handrails are installed with screws counterbored into the lower curve of the molding, extending into wall studs. The counterbores are then plugged and trimmed back to match the curve of the molding. If the wall material is crushable (drywall, e.g.), there will be a backer board installed into the wall to provide sufficient strength; if the wall material is sufficiently firm (cement plaster over wooden lathe, e.g.) the backer may be ommitted.
So, for your rail to be loose, one or more of three things is going on:
- The screws have come loose (or even broken) in the studs.
- The screw heads have "wallowed" out the molding.
- The wall material has been crushed by downward pressure on the rail translating into lateral pressure on the wall, bending the screws and promoting wallowing of the screw heads.
Looking at the picture, it's not completely clear, but I think there is a backing board in your installation.
There is no easy fix. It's not even clear that you can salvage the existing rail. To do the job right, you'd have to find and remove the all the screws holding the rail, repair the holes in the molding and if there is damaging to the screw holding power of the studs, repair that as well, then remount the rail, replug the holes, trim, sand, and repaint. You'd likely be better served with a new molding, rather than repairing the old.
You may able to avoid the full repair above by creating new mounting holes in the molding, above the originals, and slanted somewhat upward into the studs. But it's going to be difficult to get the railing pulled back tight against the wall or backer with the old screws in there, and if there is any damage to the wall or backer, it's not going to be a very robust mount. And you'd still have all the work of plugging and fill and sanding the new mounting holes.
Or, if you just want it fixed for safety reasons, and don't care about the esthetics, you could try the latter approach without going to the trouble of counterboring, plugging, and filling, using self drilling construction screws through the foot of the molding upward into the studs. Won't be pretty, but it could provide an adequate mechanical solution.