Adding position: relative to the parent, and a max-height:100%; on div2 works.
<body>
<div id="div1" style="height: 500px;position:relative;">
<div id="div2" style="max-height:100%;overflow:auto;border:1px solid red;">
<div id="div3" style="height:1500px;border:5px solid yellow;">hello</div>
</div>
</div>
</body>
Update: The following shows the “updated” example and answer. http://jsfiddle.net/Wcgvt/181/
The secret there is to use box-sizing: border-box, and some padding to make the second div height 100%, but move it’s content down 50px. Then wrap the content in a div with overflow: auto to contain the scrollbar. Pay attention to z-indexes to keep all the text selectable – hope this helps, several years later.